用java编写一个程序 用于实现Runnable接口并创建两个线程

2025-01-05 02:51:58
推荐回答(1个)
回答1:

public class TestThreadOutput {

public static void main(String[] args) {
OutputInt opi=new OutputInt();
Thread th1=new Thread(opi);
Thread th2=new Thread(opi);
th1.start();
th2.start();
}
}
class OutputInt implements Runnable{

public void run() {
for(int i=10;i<=15;i++){
System.out.print(i+",");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}