主函数部分:
public class Demo {
public static void main(String[] args) {
//创建打印数字类的对象
PrintNum pn = new PrintNum();
//以pn为参数创建三个线程
//分别命名为1号、2号、3号
Thread t1 = new Thread(pn, "1号");
Thread t2 = new Thread(pn, "2号");
Thread t3 = new Thread(pn, "3号");
//启动线程
t1.start();
t2.start();
t3.start();
}
}
PrintNum类部分:
public class PrintNum implements Runnable {
//num表示第n个数
private int num = 1;
//state用来作为某一线程是否需要wait的标志
private String state = "1号";
//N用来选择线程1还是线程2还是线程3
int N = 1;
public int getN() {
return N;
}
public void setN(int n) {
N = n;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public void run() {
while (true) {
if(this.num<=60) {
if (getN() == 1) {
printnum1();
setN(2);
} else if (getN() == 2) {
printnum2();
setN(3);
} else if (getN() == 3) {
printnum3();
setN(1);
}
}else{
break;
}
}
}
//线程1的内容
public synchronized void printnum1() {
if(!(getState().equals("1号"))){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int num;
for (num=this.num; this.num <= num + 4; this.num++) {
if (this.num <= 60) {
System.out.println(Thread.currentThread().getName() + "线程:" + this.num);
}
}
//将状态设置为线程2
setState("2号");
//唤醒线程
notifyAll();
}
//线程2的内容
public synchronized void printnum2(){
if(!(getState().equals("2号"))){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int num;
for (num=this.num; this.num <= num + 4; this.num++) {
if (this.num <= 60) {
System.out.println(Thread.currentThread().getName() + "线程:" + this.num);
}
}
//将状态设置为线程3
setState("3号");
//唤醒线程
notifyAll();
}
//线程3的内容
public synchronized void printnum3(){
if(!(getState().equals("3号"))){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int num;
for (num=this.num; this.num <= num + 4; this.num++) {
if (this.num <= 60) {
System.out.println(Thread.currentThread().getName() + "线程:" + this.num);
}
}
//将状态设置为线程1
setState("1号");
//唤醒线程
notifyAll();
}
}
1号线程:1
1号线程:2
1号线程:3
1号线程:4
1号线程:5
1号线程:6
1号线程:7
1号线程:8
1号线程:9
1号线程:10
1号线程:11
1号线程:12
1号线程:13
1号线程:14
1号线程:15
1号线程:16
1号线程:17
1号线程:18
1号线程:19
1号线程:20
1号线程:21
1号线程:22
1号线程:23
1号线程:24
1号线程:25
1号线程:26
1号线程:27
1号线程:28
1号线程:29
1号线程:30
1号线程:31
1号线程:32
1号线程:33
1号线程:34
1号线程:35
1号线程:36
1号线程:37
1号线程:38
1号线程:39
1号线程:40
1号线程:41
1号线程:42
1号线程:43
1号线程:44
1号线程:45
1号线程:46
1号线程:47
1号线程:48
1号线程:49
1号线程:50
1号线程:51
1号线程:52
1号线程:53
1号线程:54
1号线程:55
1号线程:56
1号线程:57
1号线程:58
1号线程:59
1号线程:60
结果输出的全是线程一的1-60,而且没有使进程停止。
我的理解是:
一开始因为有int N = 1;所以run()里执行的是 if (getN() == 1) 的代码块,
里面则是调用的同步方法快printnum1(),因为是线程t1先开始所以应该由它抢到执行权,
当完成前5个打印语句后,将state改为"2号",N改为2,那么这个while循环应该要重新循环了,并且应该直接去if (getN() == 2)的代码块才对。
此时却仍然是Thread.currentThread().getName()为1号。
因此我第一个疑惑是为什么一直是1个线程在抢执行权,但是其他两个没有动?
而当最后一个for循环结束前应该也会有this.num++的动作才对,这样才可以在run()里执行break语句,可是并没有退出循环。
因此我第二个疑惑是程序没有直接退结束,而是一直处于调试状态的原因究竟是为何呢?
请大神们给一个解决方法的思路,谢谢!