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次,
但是程序不循环,只输出了一次,接触多线程不久,不知道发生了什么,
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) {
} }}
* @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();
} }}
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
而3楼的代码是每个线程一次只能输出一个字母.看结果你就知道了, A, B, C是三个线程交替输出的.
在2楼的代码中, 那个notify()根本没有任何作用.原以为你是想要知道怎么去使用wait(), nofity()呢.
也就是说ID输出成1,2,3,1,2,3这种。
另外2楼的代码,删掉notify和不删掉应该是很不一样的吧,呵呵
要学会多思考, 其实我在给你回答问题的时候, 也从中学到不少东西, 即便是复习, 也加深了印象, 互相学习.