我试图在 java 中创建几个流,我的程序必须创建 3 个线程和 1 个主线程,然后停止。
我创建了一个实现了 Runnable 的类
class NewThread implements Runnable {
String name;
Thread t;
NewThread(String threadname){
name = threadname;
t = new Thread (this, name);
System.out.println(t);
t.start();
}
public void run (){
try {
System.out.println("111");// why cant see it?
Thread.sleep(1000);
}
catch (InterruptedException e){
System.out.println(e);
}
System.out.println("End Thread");
}
主要:
public class ThreadDemo {
public static void main (String []args){
new Thread ("F");
new Thread ("S");
new Thread ("T");
try {
Thread.sleep(10000);
}
catch (InterruptedException e){
}
System.out.println("End M");
}
}
我想我会得到像 3 个字符串 111 和一个字符串 End M -
111
111
111
End M
但我只是
End M
谁能告诉我为什么我的程序结果没有得到 3 个字符串?
最佳答案
您需要创建 NewThread
实例而不是通用 Threads
来执行代码:
new NewThread("F");
...
关于java - Java中三个线程的合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209462/