我有两个线程:C和D.如果我想线程C-先执行,那么线程d,这是我必须做的:如何让一个线程等待另一个线程?
这是ThreadC
类代码:
public class ThreadC implements Runnable {
int isi[] = new int[100];
public void run() {
for (int i = 0;i < isi.length; i++) {
isi[i] = i;
System.out.print(isi[i] + " ");
}
}
}
这为ThreadD
类代码:
public class ThreadD implements Runnable {
int temp=0;
public void run() {
for(int i = 0; i < 1000; i++)
temp += i;
System.out.println("nilai temp : " + temp);
}
}
这是主要的方法:
public class main {
public static void main (String[] args) {
ThreadC tc = new ThreadC();
Thread t1 = new Thread(tc);
t1.start();
ThreadD td = new ThreadD();
Thread t2 = new Thread(td);
t2.start();
}
}
编辑:
我其实有问题,我有课。首先我打电话给ThreadC。我想要线程C完成执行,然后线程将执行。然后我打电话给ThreadD。它像下面的代码:
for (int j = 0; j < idW.length; j++) {
webtext = d.getWebText(idW[j]);
ThreadPrepo tpo = new ThreadPrepo(webtext, host[j%jumhost], "server", 1099, idW[j]);
Thread t1 = new Thread(tpo);
t1.start();
}
ThreadD td = new ThreadD;
Thread t2 = new Thread(t2);
t2.start();
所以ThreadD T2将线程T1完成执行后执行,因此线程T2必须等到T1哈希完成。
我该如何解决这个问题?
如果它是'Runnable',则不要将其称为线程。在你的两个'Runnable'实例之间同步你的'run()'方法代码。第一个输入块将首先执行并完成。 –
为什么大家试图用线程做的第一件事就是首先破坏线程的整个观点? –
我从来不理解这些问题。如果你想顺序执行,为什么你使用线程呢? – EJP