Topic: 求教:多线程同步代码块和同步方法之间实现同步的问题! |
Print this page |
1.求教:多线程同步代码块和同步方法之间实现同步的问题! | Copy to clipboard |
Posted by: drxjava Posted on: 2004-10-24 10:24 是两段差不多的代码,都是在多线程同步中的同步代码块和同步方法之间实现同步的代码。 可是代码1能够实现同步代码块和同步方法之间的同步,并且能够启动两个线程。 但是,代码2却不能够启动两个线程,只有第一个线程启动了呀,并且他执行的只是同步方法中的代码,没有执行同步代码块中的代码。 为什么代码2中的第二个线程不能够启动呢?我都做了对比没有发现原因,只有请高手给我这个菜鸟指点一下吧!谢谢拉!:) 代码1: class ThreadD1 { public static void main(String [] args) { TestThread tt=new TestThread(); new Thread(tt).start(); try{Thread.sleep(10);}catch(Exception e){} tt.str=new String("method"); new Thread(tt).start(); } } class TestThread implements Runnable { int tickets = 100; String str = new String(""); public void run() { if(str.equals("method")) { while(true) { synchronized(this) { if(tickets>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName()+"is selling ticket"+tickets--); } } } } else { while (true) { sale(); } } } public synchronized void sale() { if(tickets>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.print("sale():"); System.out.println(Thread.currentThread().getName()+"is selling ticket"+tickets--); } } } 代码2: class ThreadDo1 { public static void main(String [] args) { TestThread tt=new TestThread(); new Thread(tt).start(); try{Thread.sleep(1);} catch(Exception e) { System.out.println("Note:Exception!!"); } tt.str=new String("drx"); new Thread(tt).start(); } } class TestThread implements Runnable { int tickets=100; String str=new String(""); public void run() { if(str.equals("drx")) { while(true) { synchronized (this) { if(tickets>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.print("成功了!"); System.out.println(Thread.currentThread().getName()+"is selling tickets"+tickets--); } } } } else { while(true) { sale(); } } } public synchronized void sale() { while(true) if(tickets>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.print("sale()haha"); System.out.println(Thread.currentThread().getName()+"is selling tickets"+tickets--); } } } |
2.Re:求教:多线程同步代码块和同步方法之间实现同步的问题! [Re: drxjava] | Copy to clipboard |
Posted by: archonLing Posted on: 2004-10-24 12:01 drxjava wrote: The while(true) in your sale() triggers the java monitor and does not let go, so any additional synchronized methods or blocks executed afterward will be waiting forever. In short, make sure you exit the synchronized block or method after you are done. |
3.Re:求教:多线程同步代码块和同步方法之间实现同步的问题! [Re: drxjava] | Copy to clipboard |
Posted by: drxjava Posted on: 2004-10-24 13:45 Thankyou very much! I was understanded it! |
4.Re:求教:多线程同步代码块和同步方法之间实现同步的问题! [Re: drxjava] | Copy to clipboard |
Posted by: drxjava Posted on: 2004-10-24 17:23 哎呀!我还有一个地方不明白呀! 就是再代码2中的: if(str.equals("drx")) { while(true) { synchronized (this)<-----就是这里,把this换成str对象,结果第二个线程就能够被启动了,虽然没有实现同步,可是线程却被启动了。 { if(tickets>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.print("成功了!"); System.out.println(Thread.currentThread().getName()+"is selling tickets"+tickets--); } } } } 如果说上面的代码2没有启动第二个线程是因为上面的高手所说的由于 while(true)的缘故,可是为什么这里改为了str对象就又能够启动了第二个线程了呢? 请大虾帮帮我吧!谢谢了呀!:) |
5.Re:求教:多线程同步代码块和同步方法之间实现同步的问题! [Re: drxjava] | Copy to clipboard |
Posted by: archonLing Posted on: 2004-10-24 21:53 If you use synchronized (str), then the java monitor is on the specfic variable. As long as there is not any other monitor set up on the variable, the thread will execute. But the point is why put the monitor on str. If "tickets" is what you want to synchronize, then make it an "Integer" object and put sychronized(tickets) instead. As a sidenote, if you use jdk1.5.0 to compile, you can use unary operator on the Integer object just like the primitive types. |
6.Re:求教:多线程同步代码块和同步方法之间实现同步的问题! [Re: drxjava] | Copy to clipboard |
Posted by: drxjava Posted on: 2004-10-25 12:20 谢谢,这下彻底的明白了呀!关键是监视器的问题,呵呵! 谢谢呀!:) |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |