今天写了个多线程小程序,抽出主要代码贴在下面了,但运行结果和我想的不一样,看看有没有高手能给解决一下.大体思路是这样的:
1) 我先在 main 函数里创建了一个我自己写的 CrticalResource(临界资源)类的对象 cr;
2) 然后创建了 3 个线程,名字分别为 t[0] , t[1] , t[2] ;
3) 依次启动 3 个线程;
4) 每个线程启动后,会默认调用 run() 方法,在 run() 方法中循环执行 3 次 cr.exec() 方法;
5) CrticalResource 类中 exec() 方法用来模拟对临界资源的操作,变量 mutex 用来决定操作临界资源的线程的顺序.
mutex 的初值为 " t[0] ",在 exec() 中如果 当前线程名 == mutex 时输出 "运行进程: t[0] ", mutex 的值变为 " t[1] ",
并在操作完毕后先输出 "结束进程: t[0] ",然后 notify() ,否则当前线程 wait() .
依次类推循环执行 3 次,且当 mutex=" t[3] "时让 mutex=" t[0] ".
我期望的结果是依次重复 3 次输出:
运行线程:t[0]
结束线程:t[0]
运行线程:t[1]
结束线程:t[1]
运行线程:t[2]
结束线程:t[2]但输出的结果数却总是不够,总是没有重复 3 次就不输出了,但程序还没有结束.百思不得其解,望高人相助.源码如下,大家研究研究:// TestThreadWaitAndNotify 类
public class TestThreadWaitAndNotify { public static void main(String args[]){
CrticalResource cr = new CrticalResource();
CreateThread t[] = {
new CreateThread("t[0]", cr),
new CreateThread("t[1]", cr),
new CreateThread("t[2]", cr)
};
for(CreateThread tr : t)
tr.start();
}
}
// CreateThread 类
public class CreateThread extends Thread {
CrticalResource cr;
CreateThread(String name, CrticalResource cr){
super(name);
this.cr = cr;
}
public void run(){
for(int i = 0; i < 3; ++i){
synchronized(cr){
cr.exec();
}
}
}
}
// CrticalResource 类
public class CrticalResource { private String mutex = "t[0]";
synchronized void exec(){
if(!mutex.equals(Thread.currentThread().getName())){
try{
wait();
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
else{
System.out.println("运行线程:" + Thread.currentThread().getName());
try{
Thread.sleep(1);
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
int i = 1 + Integer.parseInt(Thread.currentThread().getName().substring(2, 3));
i %= 3;
mutex = "t[" + i + "]";
System.out.println("结束线程:" + Thread.currentThread().getName());
notifyAll();
}
}
}
1) 我先在 main 函数里创建了一个我自己写的 CrticalResource(临界资源)类的对象 cr;
2) 然后创建了 3 个线程,名字分别为 t[0] , t[1] , t[2] ;
3) 依次启动 3 个线程;
4) 每个线程启动后,会默认调用 run() 方法,在 run() 方法中循环执行 3 次 cr.exec() 方法;
5) CrticalResource 类中 exec() 方法用来模拟对临界资源的操作,变量 mutex 用来决定操作临界资源的线程的顺序.
mutex 的初值为 " t[0] ",在 exec() 中如果 当前线程名 == mutex 时输出 "运行进程: t[0] ", mutex 的值变为 " t[1] ",
并在操作完毕后先输出 "结束进程: t[0] ",然后 notify() ,否则当前线程 wait() .
依次类推循环执行 3 次,且当 mutex=" t[3] "时让 mutex=" t[0] ".
我期望的结果是依次重复 3 次输出:
运行线程:t[0]
结束线程:t[0]
运行线程:t[1]
结束线程:t[1]
运行线程:t[2]
结束线程:t[2]但输出的结果数却总是不够,总是没有重复 3 次就不输出了,但程序还没有结束.百思不得其解,望高人相助.源码如下,大家研究研究:// TestThreadWaitAndNotify 类
public class TestThreadWaitAndNotify { public static void main(String args[]){
CrticalResource cr = new CrticalResource();
CreateThread t[] = {
new CreateThread("t[0]", cr),
new CreateThread("t[1]", cr),
new CreateThread("t[2]", cr)
};
for(CreateThread tr : t)
tr.start();
}
}
// CreateThread 类
public class CreateThread extends Thread {
CrticalResource cr;
CreateThread(String name, CrticalResource cr){
super(name);
this.cr = cr;
}
public void run(){
for(int i = 0; i < 3; ++i){
synchronized(cr){
cr.exec();
}
}
}
}
// CrticalResource 类
public class CrticalResource { private String mutex = "t[0]";
synchronized void exec(){
if(!mutex.equals(Thread.currentThread().getName())){
try{
wait();
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
else{
System.out.println("运行线程:" + Thread.currentThread().getName());
try{
Thread.sleep(1);
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
int i = 1 + Integer.parseInt(Thread.currentThread().getName().substring(2, 3));
i %= 3;
mutex = "t[" + i + "]";
System.out.println("结束线程:" + Thread.currentThread().getName());
notifyAll();
}
}
}
while (!mutex.equals(Thread.currentThread().getName())) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
System.out.println("运行线程:" + Thread.currentThread().getName());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
int i = 1 + Integer.parseInt(Thread.currentThread().getName()
.substring(2, 3));
i %= 3;
mutex = "t[" + i + "]";
System.out.println("结束线程:" + Thread.currentThread().getName());
notifyAll();
}
wait的时候不要用if,要用while
还是帮你顶下。