import java.lang.*;
import java.util.*;public class helloout 
{ /**
 * @param args
 */
public static void main(String[] args) 
{ Thread th1=new mythread();
th1.start();
Thread th2=new mythread();
th2.start();
Thread th3=new mythread();
th3.start(); }

}class mythread  extends Thread
{
private static String str="C";
  private static int i=0;
public mythread()
{

}
public synchronized void run()
{
try{
while(i<10)
{
notifyAll();
if(str.endsWith("C"))
{
System.out.println("A");
str=str + "A";
wait();
}
if(str.endsWith("A"))
{
System.out.println("B");
str=str + "B";
wait();
}
if(str.endsWith("B"))
{
System.out.println("C");
str=str + "C";
i++;
wait();
}
}
}
catch(Exception e)
{
}

}


}本来是想循环输出A,B,C,A,B,C这种顺序10次,
但是程序不循环,只输出了一次,接触多线程不久,不知道发生了什么,


解决方案 »

  1.   

    package test;import java.lang.*;
    import java.util.*;public class helloout { /**
     * @param args
     */ public static void main(String[] args) { Thread th1 = new mythread();
    th1.start();
    Thread th2 = new mythread();
    th2.start();
    Thread th3 = new mythread();
    th3.start(); }}class mythread extends Thread {
    private static String str = "C";
    private static int i = 0; public mythread() { } public synchronized void run() {
    try {
    while (i < 10) {
    synchronized (helloout.class) { if (str.endsWith("C")) {
    System.out.println("A");
    str = str + "A";
    //System.out.println("str=" + str);

    }
    if (str.endsWith("A")) {
    System.out.println("B");
    str = str + "B";

    }
    if (str.endsWith("B")) {
    System.out.println("C");
    str = str + "C";
    i++; }
    notify();
    }
    }
    } catch (Exception e) {
    } }}
      

  2.   

    public class Test { /**
     * @param args
     */ public static void main(String[] args) {
    Thread th1 = new mythread(1);
    th1.start();
    Thread th2 = new mythread(2);
    th2.start();
    Thread th3 = new mythread(3);
    th3.start();
    }
    }class mythread extends Thread {
    public static String str = "C";
    private static int i = 0;
    private int id = 0; public mythread(int id) {
    this.id = id;
    } public void run() {
    try {
    // 三个线程要使用同一个共享对象来进行加锁和解锁.
    // 方法前面使用synchronized使用的是当前对象来加锁, 所以三个不同的线程他们的对象锁是不一样的.
    synchronized (this.getClass()) {
    while (i < 10) {
    this.getClass().notifyAll();
    if (str.endsWith("C")) {
    System.out.println("ID:" + id + ", Times:" + i + " : A");
    str = str + "A";
    this.getClass().wait();
    } if (str.endsWith("A")) {
    System.out.println("ID:" + id + ", Times:" + i + " : B");
    str = str + "B";
    this.getClass().wait();
    } if (str.endsWith("B")) {
    System.out.println("ID:" + id + ", Times:" + i + " : C");
    str = str + "C";
    i++;
    this.getClass().wait();
    } }
    }
    } catch (Exception e) {
    e.printStackTrace();
    } }}
      

  3.   

    为了使得结果更直观, 修改了点代码, 输出结果如下:
    ID:1, Times:0 : A
    ID:2, Times:0 : B
    ID:1, Times:0 : C
    ID:3, Times:1 : A
    ID:1, Times:1 : B
    ID:2, Times:1 : C
    ID:3, Times:2 : A
    ID:2, Times:2 : B
    ID:1, Times:2 : C
    ID:3, Times:3 : A
    ID:1, Times:3 : B
    ID:2, Times:3 : C
    ID:3, Times:4 : A
    ID:2, Times:4 : B
    ID:1, Times:4 : C
    ID:3, Times:5 : A
    ID:1, Times:5 : B
    ID:2, Times:5 : C
    ID:3, Times:6 : A
    ID:2, Times:6 : B
    ID:1, Times:6 : C
    ID:3, Times:7 : A
    ID:1, Times:7 : B
    ID:2, Times:7 : C
    ID:3, Times:8 : A
    ID:2, Times:8 : B
    ID:1, Times:8 : C
    ID:3, Times:9 : A
    ID:1, Times:9 : B
    ID:2, Times:9 : C
      

  4.   

    2楼的代码是每个线程一次性输出ABC, 然后下一个线程再输出ABC, 即每个线程一次要输出3个字母.
    而3楼的代码是每个线程一次只能输出一个字母.看结果你就知道了, A, B, C是三个线程交替输出的.
    在2楼的代码中, 那个notify()根本没有任何作用.原以为你是想要知道怎么去使用wait(), nofity()呢.
      

  5.   

    没有删掉嘛~~~ 哈哈哈  没有wait 肯定没有nofity哇
      

  6.   

    其实我真正的目的是想知道怎么按一定的顺序调用同一个线程类的不同实例线程,只是我的例子写的不好,
    也就是说ID输出成1,2,3,1,2,3这种。
    另外2楼的代码,删掉notify和不删掉应该是很不一样的吧,呵呵
      

  7.   

    也就是说唤醒一个特定的正在阻塞 的线程,并将当前线程阻塞,sigh,绕了一大圈就是为了这个,呵呵
      

  8.   

    不会停, 因为会有最后一个线程在等待而没有其他线程去唤醒他了. 要让他不停, 你就得再去写其他的语句来控制最后的结束方式. 比如在每个线程里加一个结束标志, 例如他的执行次数, 当他自己的执行次数到9后, 就不再调用wait方法, 这样就可以实现.
    要学会多思考, 其实我在给你回答问题的时候, 也从中学到不少东西, 即便是复习, 也加深了印象, 互相学习.