首页 > 代码库 > java 线程
java 线程
java 线程创建code如下:
public class My_runnable{
public static void main(String[] args){
Thread t = new Thread(new Runnable(), "aaa"); //aaa is the thread name
t.start(); //会调用Runnable中的run 方法,该方法在My_runnable中实现
int i = 0;
for(;;){
Date date = new Date();
String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(date);
System.out.println(str + ":main thread: " + i);
i++;
if(i == 5)
break;
}
}
@Override
public void run(){
int i = 0;
while(true){
Date date = new Date();
String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(date);
System.out.println(str + ":main thread: " + i);
i++;
if(i == 5)
break;
}
}
}