线程是否在 wait() 之后唤醒并再次获得监视器从 wait() 之后的下一条指令继续或从头开始继续?
Does a thread waked after wait() and got the monitor again continues from the next instruction after wait() or from the beginning?
假设 thread_1
这样做:
synchronized(o) {
dosomthing1;
o.wait();
dosomthing2;
}
假设 thread_1 被唤醒,在获取 o
监视器时被阻止,明白了。下一个 thread_1
指令是什么? dosomthing1
或 dosomthing2
?
假设我们有 2 种方法printFirst()
将打印从 0 到 5,printSecond()
将打印从 0 到 10
void printFirst() {
for (int i = 0; i <= 5; i++) {
System.out.print(i + " ");
}
}
void printSecond() {
for (int i = 0; i <= 10; i++) {
System.out.print(i + " ");
}
}
这两个方法将由 Thread-0
使用,正如您首先在 try catch
中看到的那样,首先它将在方法完成其工作后调用 printFirst
方法 wait()
方法会将当前放在 WAIT STATE
上, wait()
下面的任何代码都不会执行,直到 notify
被调用。
synchronized void testThreadWait(){
System.out.println(Thread.currentThread().getName() +" starting to work");
try {
printFirst();
wait();
System.out.println(Thread.currentThread().getName() + "starts");
printSecond();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
另一个方法将从 Thread-1 执行,它会打印一些东西,它会 notify
() 处于当前 wait
状态的线程
synchronized void testThreadNotify(){
System.out.println("\n" + Thread.currentThread().getName() + "starting to work");
System.out.println("Thread one is waiting");
System.out.println("Preparing to notify Thread One");
notify();
}
当 wait() 方法被调用时 Thread-0
将继续执行他的工作,在这种情况下将调用 printSecond()
wait() 方法下面的方法,并且它将打印来自0-10
完整代码
class TestThread{
void printFirst() {
for (int i = 0; i <= 5; i++) {
System.out.print(i + " ");
}
}
void printSecond() {
for (int i = 0; i <= 10; i++) {
System.out.print(i + " ");
}
}
synchronized void testThreadWait(){
System.out.println(Thread.currentThread().getName() +" starting to work");
try {
printFirst();
wait();
System.out.println(Thread.currentThread().getName() + "starts");
printSecond();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("This will be printed after wait");
}
synchronized void testThreadNotify(){
System.out.println(Thread.currentThread().getName() + "starting to work");
System.out.println("Thread one is waiting");
System.out.println("Preparing to notify Thread One");
notify();
}
}
public class Test{
public static void main(String args[]){
final TestThread c=new TestThread();
new Thread(){
public void run(){c.testThreadWait();}
}.start();
new Thread(){
public void run(){c.testThreadNotify();}
}.start();
}
}
输出
Thread-0 starting to work
0 1 2 3 4 5
Thread-1starting to work
Thread one is waiting
Preparing to notify Thread One
Thread-0starts
0 1 2 3 4 5 6 7 8 9 10 <--- prints only printSecond()
在这里你可以看到,当 Thread-0 被通知时,它只打印 wait()
方法下面的第二个方法
假设 thread_1
这样做:
synchronized(o) {
dosomthing1;
o.wait();
dosomthing2;
}
假设 thread_1 被唤醒,在获取 o
监视器时被阻止,明白了。下一个 thread_1
指令是什么? dosomthing1
或 dosomthing2
?
假设我们有 2 种方法printFirst()
将打印从 0 到 5,printSecond()
将打印从 0 到 10
void printFirst() {
for (int i = 0; i <= 5; i++) {
System.out.print(i + " ");
}
}
void printSecond() {
for (int i = 0; i <= 10; i++) {
System.out.print(i + " ");
}
}
这两个方法将由 Thread-0
使用,正如您首先在 try catch
中看到的那样,首先它将在方法完成其工作后调用 printFirst
方法 wait()
方法会将当前放在 WAIT STATE
上, wait()
下面的任何代码都不会执行,直到 notify
被调用。
synchronized void testThreadWait(){
System.out.println(Thread.currentThread().getName() +" starting to work");
try {
printFirst();
wait();
System.out.println(Thread.currentThread().getName() + "starts");
printSecond();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
另一个方法将从 Thread-1 执行,它会打印一些东西,它会 notify
() 处于当前 wait
状态的线程
synchronized void testThreadNotify(){
System.out.println("\n" + Thread.currentThread().getName() + "starting to work");
System.out.println("Thread one is waiting");
System.out.println("Preparing to notify Thread One");
notify();
}
当 wait() 方法被调用时 Thread-0
将继续执行他的工作,在这种情况下将调用 printSecond()
wait() 方法下面的方法,并且它将打印来自0-10
完整代码
class TestThread{
void printFirst() {
for (int i = 0; i <= 5; i++) {
System.out.print(i + " ");
}
}
void printSecond() {
for (int i = 0; i <= 10; i++) {
System.out.print(i + " ");
}
}
synchronized void testThreadWait(){
System.out.println(Thread.currentThread().getName() +" starting to work");
try {
printFirst();
wait();
System.out.println(Thread.currentThread().getName() + "starts");
printSecond();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("This will be printed after wait");
}
synchronized void testThreadNotify(){
System.out.println(Thread.currentThread().getName() + "starting to work");
System.out.println("Thread one is waiting");
System.out.println("Preparing to notify Thread One");
notify();
}
}
public class Test{
public static void main(String args[]){
final TestThread c=new TestThread();
new Thread(){
public void run(){c.testThreadWait();}
}.start();
new Thread(){
public void run(){c.testThreadNotify();}
}.start();
}
}
输出
Thread-0 starting to work
0 1 2 3 4 5
Thread-1starting to work
Thread one is waiting
Preparing to notify Thread One
Thread-0starts
0 1 2 3 4 5 6 7 8 9 10 <--- prints only printSecond()
在这里你可以看到,当 Thread-0 被通知时,它只打印 wait()
方法下面的第二个方法