我有一个小问题。
我有两个主题:
HelloThread
- 这打印"你好"五次。GoodbyeThread
- 打印" Goodbye"五次。我希望HelloThread
首先运行,然后运行GoodbyeThread
运行。
我已经用信号量解决了这个问题(但信号量并不是真正的java方式,它更像是C方式)。
HelloThread.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package modifiedthreadhellogoodbye;
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.Semaphore;
class HelloThread implements Runnable {
private final Object lock;
//private final Semaphore lock;
public HelloThread(Semaphore lock) {
this.lock = lock;
}
public HelloThread(Object lock) {
this.lock = lock;
}
@Override
public void run() {
int pause;
synchronized (lock) {
for (int i = 0; i < 5; i++) {
System.out.println("Hello!");
pause = (int) (Math.random() * 1000);
try {
sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(HelloThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
lock.notifyAll();
System.out.println("Outsite hello");
}
//lock.release();
}
}
GoodbyeThread.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package modifiedthreadhellogoodbye;
import static java.lang.Thread.sleep;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
class GoodbyeThread implements Runnable {
int pause;
//private final Semaphore lock;
private final Object lock;
public GoodbyeThread(Semaphore lock) {
this.lock = lock;
}
public GoodbyeThread(Object lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
System.out.println("Inside the synchronized");
try {
lock.wait();
} catch (InterruptedException ex) {
Logger.getLogger(GoodbyeThread.class.getName()).log(Level.SEVERE, null, ex);
}
//lock.acquire();
for (int i = 0; i < 5; i++) {
System.out.println("Goodbye");
pause = (int) (Math.random() * 1000);
try {
sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(GoodbyeThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
这是我的运行线程的类:
public class ModifiedThreadObject {
private final Object lock = new Object();
//private final Semaphore lock = new Semaphore(0);
public ModifiedThreadObject() {
HelloThread hello = new HelloThread(lock);
GoodbyeThread goodbye = new GoodbyeThread(lock);
Thread t1 = new Thread(hello);
Thread t2 = new Thread(goodbye);
t1.start();
t2.start();
}
}
来自GoodbyeThread
的信号的主要想法是wait()
应该HelloThread
如果首先运行GoodbyeThread
,它可以正常运行,但HelloThread
先运行我有以下输出:
Hello!
Hello!
Hello!
Hello!
Hello!
Outsite hello
Inside the synchronized
HelloThread
发送一个notifyAll()
,但没有人在等待,所以&#34;信号&#34;失去了......
有人有想法吗?
答案 0 :(得分:7)
首先,我在这里一直质疑单独线程的使用。如果你想要一个接一个的事情发生,只需使用一个线程。
然而,很容易等到一个线程完成后 - 只需使用join
:
Thread t1 = new Thread(hello);
Thread t2 = new Thread(goodbye);
t1.start();
t1.join();
t2.start();
这样你就不需要在&#34;你好&#34;或者&#34;再见&#34;代码。
如果您想要更复杂的内容,我建议您查看java.util.concurrent
包。虽然可以使用wait()
和notify()
,但使用更高级别的构造通常更好。例如,对于生产者/消费者场景,您可能希望使用BlockingQueue
而不是自己实现它。
答案 1 :(得分:3)
您可以在CountDownLatch
的帮助下实现这一目标,简单示例:
import java.util.concurrent.CountDownLatch;
public class Test {
public static void main(String... s){
final CountDownLatch cdl = new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
int i = 5;
while((i--)>0)
System.out.println("hello");
cdl.countDown();
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
cdl.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("goodbye");
}
});
t2.start();
t1.start();
}
}
答案 2 :(得分:0)
虽然John Skeet提供了正确的方法来执行此类操作,但确切了解程序流错误也是有用的。
在GoodbyeThread
:
synchronized (lock) {
System.out.println("Inside the synchronized");
try {
lock.wait();
}
那么,在收到锁之后,你坚持要等吗?这个问题是HelloThread
已经在GoodbyeThread
获取它的时候终止了;当HelloThread
调用notifyAll
时,这没有任何作用,因为还没有人在等待锁定。
另一个问题是使用Semaphore
。如上所述,您的课程不会使用Semaphore
的任何功能,事实上,无论如何都会将其转换为Object
。您应该从代码中删除所有提及Semaphore
的内容,因为这是不必要的,可能会造成混淆。