- //死锁同代码中同步函数
- package thread;
- class LockTwo implements Runnable{
-
- public static boolean falg;
- private static int ticket = 100;
-
- LockTwo(boolean falg) {
-
- this.falg = falg;
- }
-
- public void run(){
-
- if(falg){
- while(true)
- synchronized(LockTwo.class){
-
- show();
- }
- }
- else{
- while(true)
- show();
- }
-
- }
- private synchronized void show(){
-
- synchronized(LockTwo.class){
-
- System.out.println(Thread.currentThread().getName()+"----------------"+ticket--);
- }
- }
- }
- public class DeathLockTwo{
-
- public static void main(String[] args){
-
- LockTwo lock = new LockTwo(false);
- new Thread(lock).start();
- lock.falg = true;
- new Thread(lock).start();
- }
- }
复制代码
此程序为什么构不成死锁呢?请高手解答。谢谢。 |