我想了解如何使用关键字:wait、notify/All、synchronized,所以我决定尝试一个简单的示例。基本上我想做的是创建两个将打印字符串的线程。第一个线程具有字符串“Hello”,而第二个线程具有字符串“World”。
我想要达到的输出如下: 你好 世界 你好 世界 你好 世界 ...
这是我到目前为止编写的代码,但现在的输出是: 你好 你好 你好 ... 世界 世界 世界 ...
错误在哪里?谢谢。 :)
代码如下:
class MyThread implements Runnable {
private SimpleSyncThread sync;
private String s;
public MyThread(SimpleSyncThread sync, String s) {
this.sync = sync;
this.s = s;
}
public static void pause(long time) {
try {Thread.sleep(time); }
catch (InterruptedException e) {Thread.currentThread().interrupt();}
}
@Override
public void run() {
synchronized (sync) {
for (int i = 0; i < 10; i++) {
sync.print(s);
}
}
}
}
public class SimpleSyncThread {
public void print(String s) {
System.out.println(s);
MyThread.pause(200);
}
public static void main(String[] args) {
SimpleSyncThread sync = new SimpleSyncThread();
MyThread st1 = new MyThread(sync, "Hello");
MyThread st2 = new MyThread(sync, "World");
Thread t1 = new Thread(st1);
Thread t2 = new Thread(st2);
t1.start();
t2.start();
}
}
最佳答案
您在这里持有锁,因此一次只有一个进程可以打印
synchronized (sync) {
for (int i = 0; i < 10; i++) {
sync.print(s);
}
}
您可以使用以下方法暂时释放锁定,而不是这样做:
synchronized (sync) {
for (int i = 0; i < 10; i++) {
sync.print(s);
// I have done my bit, wake other threads.
sync.notifyAll();
try {
// give up the lock and let another thread run.
sync.wait(10);
} catch(InterruptedException ie) {
throw new AssertionError(ie);
}
}
}
<小时/>
您可能想到的是我所说的乒乓球测试。您不会在真实的程序中执行此操作,但此模式可以提供有用的微基准。
public class PingPongMain {
public static void main(String[] args) throws InterruptedException {
boolean[] next = {false};
AtomicInteger count = new AtomicInteger();
Thread t1 = new Thread(() -> {
try {
synchronized (next) {
for(;;) {
// handle spurious wake ups.
while (next[0])
next.wait();
System.out.println("ping");
// state change before notify
next[0] = true;
next.notifyAll();
}
}
} catch (InterruptedException e) {
// expected
}
});
Thread t2 = new Thread(() -> {
try {
synchronized (next) {
for(;;) {
// handle spurious wake ups.
while (!next[0])
next.wait();
System.out.println("pong");
// state change before notify
next[0] = false;
next.notifyAll();
count.incrementAndGet();
}
}
} catch (InterruptedException e) {
// expected
}
});
t1.start();
t2.start();
Thread.sleep(5000);
t1.interrupt();
t2.interrupt();
System.out.println("Ping ponged " + count + " times in 5 seconds");
}
}
打印
ping
pong
ping
pong
.. deleted ...
Ping ponged 323596 times in 5 seconds
关于java - 多线程 Java 的 Hello World,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295467/