package study;public class ThreadDemo{
public static void main(String args[]){
ThreadTest t = new ThreadTest();
new Thread(t).start(); try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
} t.str = new String("method");
new Thread(t).start();
}
}class ThreadTest implements Runnable{
private int tickets = 10;
String str = new String();
public void run(){
if(str.equals("method")){
while(true){
sale();
}
}else{
synchronized(str){
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}

}

public synchronized void sale(){
if(tickets > 0){
try{
Thread.sleep(100);
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.print("test: ");
System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
}
}
}结果怎么变到这样了??Thread-0 is saleing ticket 10
test: Thread-1 is saleing ticket 9
test: Thread-1 is saleing ticket 8
test: Thread-1 is saleing ticket 7
test: Thread-1 is saleing ticket 6
test: Thread-1 is saleing ticket 5
test: Thread-1 is saleing ticket 4
test: Thread-1 is saleing ticket 3
test: Thread-1 is saleing ticket 2
test: Thread-1 is saleing ticket 1

解决方案 »

  1.   

    synchronized的不是同一个对象吧
    synchronized一个方法=synchronized这个方法的调用者也就是this
      

  2.   

    很正常 thread-0 启动后进入了run中的同步代码块 执行了tickets--显示操作之后
    就结束了生命期 thread-1与前线程共享了一个target 进入sale同步方法显示9项后无所作为
    但程序因为while一直阻塞着 此例中没有出现互斥场合 
      

  3.   

    synchronized(str)改为synchronized(this)还是一样的结果啊.
    哪位高人帮忙给个正确code吧.谢谢.
      

  4.   

    你的的两个this不一样啊。两个ThreadTest 的对象吧?
      

  5.   

    汗 开始没看清标题 向你表示歉意
    target应该是同一个 用this可以使两者同步的
    只不过lz这种代码编排方式无法显现明显效果
    将两者的sleep语句都放在后面 且加大延时数
      

  6.   

    汗!!!还是没搞出来,下面是我改了点run()方法.
    public void run(){
    if(str.equals("method")){
    while(true){
    sale();
    }
    }else{
    synchronized(this){
    if(tickets > 0){
    try{
    Thread.sleep(10);
    }
    catch(Exception e){
    System.out.println(e.getMessage());
    }
    }
    System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
    }
    }

    } 哪位高手帮看下,能给个code最好了,谢谢了.
      

  7.   

    你的的两个this不一样啊。两个ThreadTest 的对象吧?
    所以synchronized方法肯定不可以...需要synchronized同一个对象
      

  8.   

    汗 没看懂我说的?public class Test{
        public static void main(String args[]){
            ThreadTest t = new ThreadTest();
            new Thread(t).start();
                    try{
                Thread.sleep(100);
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }        t.str = new String("method");
            new Thread(t).start();
        }
    }class ThreadTest implements Runnable{
        private int tickets = 10;
        String str = new String();
        public void run(){
            if(str.equals("method")){
                while(true){
                    sale();
                }
            }else{
                synchronized(this){
                    System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
                    try{
                        Thread.sleep(3000);
                    }
                    catch(Exception e){
                        System.out.println(e.getMessage());
                    }
               }
            }
                
        }    
        
        public synchronized void sale(){
            if(tickets > 0){
                
                System.out.print("test: ");
                System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
                try{
                    Thread.sleep(3000);
                }
                catch(Exception e){
                    System.out.println(e.getMessage());
                }
            }
        }
    }如有问题 请指出
      

  9.   

    不行啊,用您给的code结果是一样的.
    Thread-0 is saleing ticket 10
    test: Thread-1 is saleing ticket 9
    test: Thread-1 is saleing ticket 8
    test: Thread-1 is saleing ticket 7
    test: Thread-1 is saleing ticket 6
    test: Thread-1 is saleing ticket 5
    test: Thread-1 is saleing ticket 4
    test: Thread-1 is saleing ticket 3
    test: Thread-1 is saleing ticket 2
    test: Thread-1 is saleing ticket 1
      

  10.   

    你要什么结果?
    Thread-0 is saleing ticket 10 
    test: Thread-1 is saleing ticket 9 
    只要以上两句隔3秒执行就算互斥了
      

  11.   

    我想要这样的结果.这应该是同步吧?
    Thread-0 is saleing ticket 10 
    test: Thread-1 is saleing ticket 9 
    Thread-0 is saleing ticket 8 
    test: Thread-1 is saleing ticket 7 
    Thread-0 is saleing ticket 6 
    test: Thread-1 is saleing ticket 5 
    Thread-0 is saleing ticket 4 
    test: Thread-1 is saleing ticket 3 
    Thread-0 is saleing ticket 2 
    test: Thread-1 is saleing ticket 1
      

  12.   

    这是线程的交替运行 不仅仅是同步 可以利用同步实现public class Test{
        public static void main(String args[]){
            ThreadTest t = new ThreadTest();
            new Thread(t).start();
                    try{
                Thread.sleep(100);
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }        t.str = new String("method");
            new Thread(t).start();
        }
    }class ThreadTest implements Runnable{
        private int tickets = 10;
        String str = new String();
        public void run(){
            if(str.equals("method")){
                while(true){
                    sale();
                }
            }else{
                while (true) {
                    synchronized (this) {
                        if (tickets > 0) {
                            System.out.println(Thread.currentThread().getName()
                                    + " is saleing ticket " + tickets--);
                            try {
                                Thread.sleep(100);
                                this.wait();
                                this.notify();
                            }
                            catch (Exception e) {
                                System.out.println(e.getMessage());
                            }
                        }
                    }
                }
            }
                
        }    
        
        public synchronized void sale(){
            if(tickets > 0){
                
                System.out.print("test: ");
                System.out.println(Thread.currentThread().getName() + " is saleing ticket " + tickets--);
                try{
                    Thread.sleep(100);
                    this.notify();
                    this.wait();
                }
                catch(Exception e){
                    System.out.println(e.getMessage());
                }
            }
        }
    }
    result:
    Thread-0 is saleing ticket 10
    test: Thread-1 is saleing ticket 9
    Thread-0 is saleing ticket 8
    test: Thread-1 is saleing ticket 7
    Thread-0 is saleing ticket 6
    test: Thread-1 is saleing ticket 5
    Thread-0 is saleing ticket 4
    test: Thread-1 is saleing ticket 3
    Thread-0 is saleing ticket 2
    test: Thread-1 is saleing ticket 1