用Java写一个2个线程输出1-50的应用程序!

请高手帮忙!急 今天都要要! 在线等答案! 写的好的加分!
2025-02-24 23:47:22
推荐回答(2个)
回答1:

class SecondThread implements Runnable {
int i;

public void run() {
for(int i=1;i<51;i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
public class TestThread2 {
public static void main(String[] args) {
SecondThread s = new SecondThread();
Thread t = new Thread(s,"子线程1");
Thread k = new Thread(s,"子线程2");
t.start();
k.start();

}
}

回答2:

两个线程都输出这个么?

public class ThredPrint {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintT pt = new PrintT();
Thread ta = new Thread(pt);
Thread tb = new Thread(pt);
ta.start();
tb.start();
}

}

class PrintT implements Runnable {
public void run() {
for(int i = 1; i <= 50; i++) {
System.out.println(i);
try {
Thread.sleep(10);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}