- class Demo implements Runnable
- {
- private String name;
- private static int t=100;
- Demo(String name)
- {
- this.name=name;
- }
- Object obj=new Object();
- public void run()
- {
-
- synchronized(obj)
- {
- while(t>0)
- {
- try
- {
- Thread.sleep(20);
- }
-
- catch (Exception e)
- {
- }
- System.out.println(Thread.currentThread().getName()+"...run"+t--);
- }
- }
-
-
-
- }
- }
- class RunnableDemo
- {
- public static void main(String[] args)
- {
- Thread t1=new Thread(new Demo("d1"));
- Thread t2=new Thread(new Demo("d2"));
- Thread t3=new Thread(new Demo("d3"));
- Thread t4=new Thread(new Demo("d4"));
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码 对多线程操作的部分加了锁,为什么还是出现打印结果是负数的情况呢?是循环的问题么?
|
|