我需要编写一个java程序来创建一个线程来打印整数1 - 3(含)。当打印2时,线程等到第二个线程完成后再打印3.第二个线程打印字母z三次。
可能的产出包括: 1z2zz3 z1z2z3 12zzz3
我如何等待线程完成?
答案 0 :(得分:1)
这是您的任务的解决方案。有了synchronized和wait,你可以解决这个问题。
public static void main(String[] args) {
new PrintingTheads().doJob();
}
}
class PrintingTheads{
private Object objsynch= new Object();
private int numberofZ = 0;
public void doJob(){
new Thread(){
public void run(){
while(true){
System.out.print(1); Thread.yield(); // thread yeald is here for better "mixing the number 1,2 and Z letters"
System.out.print(2);Thread.yield();
synchronized (objsynch) {
while(numberofZ!=3) try{objsynch.wait(10);} catch(InterruptedException e){}
}
System.out.println(3);
try{Thread.sleep(1000);} catch(InterruptedException e){} // * this part is only for easy to see what happened and can be deleted
synchronized (objsynch) {
numberofZ = 0;
objsynch.notifyAll();
}
}
}
}.start();
new Thread(){
public void run(){
while(true){
synchronized (objsynch) {
while(numberofZ!=0) try{objsynch.wait(10);} catch(InterruptedException e){}
}
for(int i= 0;i<3;i++) {System.out.print('z');Thread.yield();}
synchronized (objsynch) {
numberofZ=3;
objsynch.notifyAll();
}
}
}
}.start();
}
}
答案 1 :(得分:0)
您可以在join
对象上调用Thread
方法等待它完成。例如:
public class JoinTest implements Runnable {
public static void main(String[] args) throws Exception {
System.out.println("in main");
Thread secondThread = new Thread(new JoinTest());
secondThread.start();
secondThread.join();
System.out.println("join returned");
}
@Override
public void run() {
System.out.println("second thread started");
// wait a few seconds for demonstration purposes
try {
Thread.sleep(3000);
} catch(InterruptedException e) {}
System.out.println("second thread exiting");
}
}
注意:您是否选择扩展Thread或为此实现Runnable并不重要 - 任何Thread
对象都可以加入。
输出应为:
in main
second thread started
然后3秒钟后:
second thread exiting
join returned
答案 2 :(得分:0)
您需要某种同步机制。 通常是信号量或互斥量。
例如
Semaphore mutex = new java.util.concurrent.Semaphore(1); // 1 - means single resource
计算线程
{
for (int i = 1; i < 3; i++) {
System.print("i");
if (i == 2) {
// wait for other thread to finish
mutex.acquire();
}
}
System.println(); // Output newline at end.
}
在&#39; zzz&#39;螺纹
{
// This should be done before the other thread starts
// and will make it wait when it wants to acquire the mutex.
mutex.acquire();
for (int i = 1; i < 3; i++) {
System.print("z");
}
// Allow the other thread to acquire the mutex.
mutex.release();
}
请原谅我的语法不是100%java,也不是异常处理。
答案 3 :(得分:0)
检查这个
public class Test {
private static int number=1;
private static int z=1;
private static Thread t2;
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i <= 20; i++) {
if (number == 3){
while (z<4);
}
System.out.print(number++);
if (number == 4){
number = 1;
z=1;
}
}
t2.stop();
}
});
t2 = new Thread(new Runnable() {
public void run() {
while(true){
while(z<=3){
System.out.print("z");
z++;
}
System.out.print("");
}
}
});
t.start();
t2.start();
}
}
答案 4 :(得分:0)
您可以将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() {
System.out.println(1);
System.out.println(2);
try {
cdl.await();//wait another thread finish
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(3);
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<3;i++)
System.out.println("z");
cdl.countDown();//notify all waiting threads
}
});
t2.start();
t1.start();
}
}
输出:1zzz23,z12zz3 ......
答案 5 :(得分:-1)
我想你想等一个线程。所以你可以把这一行放在你的run()方法中。
Thread.sleep(1000);
因此每个线程在完成执行时都会保持1秒。希望这会有效。试试并让我知道。