为什么synchronized不能同步线程?
Why synchronized can not synchronize thread?
我设置了flag的值,但是结果不是'add'和'sub'交替出现。为什么?当我查看结果时,它执行了两次 'sub' 方法。但是当 'sub' 方法结束时,标志的值将被设置为 'false'。但是结果连续打印了两次"subxxxxx"
class Resource {
private boolean flag = true;
private int num = 0;
// At here I have declared an add()
public synchronized void add() throws InterruptedException {
if (this.flag == false) {
super.wait();
}
Thread.sleep(100);
this.num++;
System.out.println("addition:"+Thread.currentThread().getName() + this.num);
this.flag = false;
super.notifyAll();
}
// At here I have declared an sub()
public synchronized void sub() throws InterruptedException {
if (this.flag == true) {
super.wait();
}
Thread.sleep(200);
this.num--;
System.out.println("subtraction:"+Thread.currentThread().getName() + this.num);
this.flag = true;
super.notifyAll();
}
}
/*
* I will test it with multiple threads. For example:
*new Thread(ad, "add").start();
*new Thread(ad, "add").start();
*new Thread(sub, "sub").start();
*new Thread(sub, "sub").start();
*When threads start. it will execute alternately. For example:
Thread add:0
Thread sub:-1
Thread add:0
Thread sub:-1
Thread add:0
Thread sub:-1
Thread add:0
Thread sub:-1
But the result is like this:
Thread add:0
Thread sub:-1
Thread sub:-2
Thread add:-1
Thread sub:-3
Thread sub:-4
Why?Why?Why?
*/
new Thread(ad, "add").start();
new Thread(ad, "add").start();
new Thread(sub, "sub").start();
new Thread(sub, "sub").start();
}
}
您似乎假设当您的 wait()
调用结束时,flag
已更改为您调用 wait()
之前想要的内容。没有这样的保证,特别是因为您涉及两个以上的线程。您应该检查是否需要继续等待。另见 Wait until boolean value changes it state
但总的来说,这些构造太低级而无法使用(除非您想了解本质),您应该查看 concurreny utils 包以获得更简单的高级构造(如队列、闩锁、条件)。
我设置了flag的值,但是结果不是'add'和'sub'交替出现。为什么?当我查看结果时,它执行了两次 'sub' 方法。但是当 'sub' 方法结束时,标志的值将被设置为 'false'。但是结果连续打印了两次"subxxxxx"
class Resource {
private boolean flag = true;
private int num = 0;
// At here I have declared an add()
public synchronized void add() throws InterruptedException {
if (this.flag == false) {
super.wait();
}
Thread.sleep(100);
this.num++;
System.out.println("addition:"+Thread.currentThread().getName() + this.num);
this.flag = false;
super.notifyAll();
}
// At here I have declared an sub()
public synchronized void sub() throws InterruptedException {
if (this.flag == true) {
super.wait();
}
Thread.sleep(200);
this.num--;
System.out.println("subtraction:"+Thread.currentThread().getName() + this.num);
this.flag = true;
super.notifyAll();
}
}
/*
* I will test it with multiple threads. For example:
*new Thread(ad, "add").start();
*new Thread(ad, "add").start();
*new Thread(sub, "sub").start();
*new Thread(sub, "sub").start();
*When threads start. it will execute alternately. For example:
Thread add:0
Thread sub:-1
Thread add:0
Thread sub:-1
Thread add:0
Thread sub:-1
Thread add:0
Thread sub:-1
But the result is like this:
Thread add:0
Thread sub:-1
Thread sub:-2
Thread add:-1
Thread sub:-3
Thread sub:-4
Why?Why?Why?
*/
new Thread(ad, "add").start();
new Thread(ad, "add").start();
new Thread(sub, "sub").start();
new Thread(sub, "sub").start();
}
}
您似乎假设当您的 wait()
调用结束时,flag
已更改为您调用 wait()
之前想要的内容。没有这样的保证,特别是因为您涉及两个以上的线程。您应该检查是否需要继续等待。另见 Wait until boolean value changes it state
但总的来说,这些构造太低级而无法使用(除非您想了解本质),您应该查看 concurreny utils 包以获得更简单的高级构造(如队列、闩锁、条件)。