首页新闻找找看学习计划

Java多线程同步块问题

0
悬赏园豆:15 [已解决问题] 解决于 2019-11-13 18:55

为什么加了同步块,还是出现了负的票数呢

//1.创建一个实例类实现Runnable接口,现象接口中的run方法,将要执行的代码放入到方法中
class thread2 implements Runnable{
//假设有一百张票在四个窗口出售
private int i=100;
@Override
public void run() {
// TODO Auto-generated method stub
Object obj=new Object();
while(true) {
synchronized(obj){
if(i>0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread()+"+"+i--);

                     }else break;
                    
                           }
                 }

}

}

//通过实现Runnable接口的方式创建线程
public class 实现runnable接口 {
public int i=100;

public static void main(String[] args) {
    // TODO Auto-generated method stub
//2.创建一个实现类对象
    thread2 th=new thread2();
//3.创建四个Thread对象,将th作为参数传入
    Thread Th1=new Thread(th);
    Thread Th2=new Thread(th);
    Thread Th3=new Thread(th);
    Thread Th4=new Thread(th);
//4.调用start方法,开启线程,调用实现接口子类中的run方法
    Th1.start();
    Th2.start();
    Th3.start();
    Th4.start();
    
    
}

}
输出结果:
Thread[Thread-0,5,main]+100
Thread[Thread-3,5,main]+99
Thread[Thread-2,5,main]+98
Thread[Thread-1,5,main]+97
Thread[Thread-0,5,main]+96
Thread[Thread-2,5,main]+95
Thread[Thread-1,5,main]+94
Thread[Thread-3,5,main]+94
Thread[Thread-0,5,main]+93
Thread[Thread-2,5,main]+92
Thread[Thread-1,5,main]+91
Thread[Thread-3,5,main]+92
Thread[Thread-0,5,main]+91
Thread[Thread-1,5,main]+90
Thread[Thread-3,5,main]+89
Thread[Thread-2,5,main]+90
Thread[Thread-0,5,main]+88
Thread[Thread-3,5,main]+87
Thread[Thread-1,5,main]+86
Thread[Thread-2,5,main]+86
Thread[Thread-0,5,main]+85
Thread[Thread-3,5,main]+84
Thread[Thread-1,5,main]+83
Thread[Thread-2,5,main]+82
Thread[Thread-0,5,main]+81
Thread[Thread-3,5,main]+80
Thread[Thread-1,5,main]+79
Thread[Thread-2,5,main]+78
Thread[Thread-0,5,main]+77
Thread[Thread-3,5,main]+76
Thread[Thread-1,5,main]+75
Thread[Thread-2,5,main]+74
Thread[Thread-0,5,main]+73
Thread[Thread-3,5,main]+72
Thread[Thread-1,5,main]+71
Thread[Thread-2,5,main]+70
Thread[Thread-3,5,main]+69
Thread[Thread-1,5,main]+68
Thread[Thread-0,5,main]+67
Thread[Thread-2,5,main]+66
Thread[Thread-1,5,main]+65
Thread[Thread-2,5,main]+63
Thread[Thread-3,5,main]+64
Thread[Thread-1,5,main]+62
Thread[Thread-0,5,main]+61
Thread[Thread-2,5,main]+60
Thread[Thread-3,5,main]+59
Thread[Thread-1,5,main]+58
Thread[Thread-0,5,main]+57
Thread[Thread-2,5,main]+56
Thread[Thread-3,5,main]+55
Thread[Thread-1,5,main]+54
Thread[Thread-0,5,main]+53
Thread[Thread-2,5,main]+52
Thread[Thread-1,5,main]+51
Thread[Thread-3,5,main]+50
Thread[Thread-0,5,main]+49
Thread[Thread-2,5,main]+48
Thread[Thread-3,5,main]+47
Thread[Thread-1,5,main]+46
Thread[Thread-0,5,main]+45
Thread[Thread-2,5,main]+44
Thread[Thread-3,5,main]+43
Thread[Thread-1,5,main]+42
Thread[Thread-0,5,main]+41
Thread[Thread-2,5,main]+40
Thread[Thread-3,5,main]+39
Thread[Thread-1,5,main]+38
Thread[Thread-0,5,main]+37
Thread[Thread-2,5,main]+36
Thread[Thread-3,5,main]+35
Thread[Thread-1,5,main]+34
Thread[Thread-0,5,main]+33
Thread[Thread-2,5,main]+32
Thread[Thread-3,5,main]+31
Thread[Thread-1,5,main]+30
Thread[Thread-0,5,main]+29
Thread[Thread-2,5,main]+28
Thread[Thread-1,5,main]+27
Thread[Thread-3,5,main]+26
Thread[Thread-0,5,main]+25
Thread[Thread-2,5,main]+24
Thread[Thread-1,5,main]+23
Thread[Thread-3,5,main]+22
Thread[Thread-0,5,main]+21
Thread[Thread-2,5,main]+20
Thread[Thread-3,5,main]+19
Thread[Thread-1,5,main]+18
Thread[Thread-0,5,main]+17
Thread[Thread-2,5,main]+16
Thread[Thread-1,5,main]+15
Thread[Thread-3,5,main]+14
Thread[Thread-0,5,main]+13
Thread[Thread-2,5,main]+12
Thread[Thread-3,5,main]+11
Thread[Thread-1,5,main]+10
Thread[Thread-0,5,main]+9
Thread[Thread-2,5,main]+8
Thread[Thread-3,5,main]+7
Thread[Thread-0,5,main]+6
Thread[Thread-1,5,main]+6
Thread[Thread-2,5,main]+5
Thread[Thread-3,5,main]+4
Thread[Thread-1,5,main]+3
Thread[Thread-0,5,main]+2
Thread[Thread-3,5,main]+1
Thread[Thread-2,5,main]+0
Thread[Thread-0,5,main]+-1
Thread[Thread-1,5,main]+-2
最后还是出现了负数

诗瑶1的主页 诗瑶1 | 初学一级 | 园豆:176
提问于:2019-11-12 19:59
< >
分享
最佳答案
0

你用对象锁,起码保证这4个线程都得用到这个对象吧,你把他放到run方法里边,不就相当于,一个线程创建一个新的对象嘛,那有什么用呢.对伐

class thread2 implements Runnable {
    private int i = 100;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (i > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread() + "+" + i--);
                } else break;
            }
        }
    }
}
收获园豆:15
森离九 | 菜鸟二级 |园豆:391 | 2019-11-13 09:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册