JAVA 线程问题!会的请进。,

来源:百度知道 编辑:UC知道 时间:2024/06/19 09:59:21
1、创建一个线程,在该线程内,间隔100毫秒输出字符串“I am a thread”;在主线程内,间隔100毫秒输出字符串“I am the main thread”。

public class T{
class TT extends Thread{
public void run(){
while(true){
System.out.println("i'm a thread");
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
}
public static void main(String[] args){
new TT().start();
while(true){
System.out.println("i am the main thread");
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
}

没有导包 你自己加上吧

public class ThreadTest{
public static void main(String[] args){
new Thread(){
public void run(){
while(true){
System.out.println("I am a thread");
try{
Thread.sleep(100);
}
catch(InterruptedException ex){}
}