当前位置: 代码迷 >> J2SE >> java基础 - 线程有关问题
  详细解决方案

java基础 - 线程有关问题

热度:106   发布时间:2016-04-23 20:45:13.0
java基础 --- 线程问题
sleep(500) ,我认为这是在 “ >= 0.5秒 ” 的时间内 本线程休息,所以,我这样写了个代码:
先看定义:
class MyThread implements Runnable{
public void run(){
}
}

接着,看个实现:
public class Test{
public static Vector<String> v = new Vector<String>() ;
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread(){
public void run() {
for( int i=0 ; i < 1000000 ; i++ ){
String s1 = ""+i ;
System.out.println("Thread["+Thread.currentThread().getName()+"]:"+s1);
ZXW_UUID_Impl.v.add(s1) ;
}
};
} , "MyThread1");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(v.size());
System.out.println(Thread.currentThread().getName());
throw new RuntimeException("-----------------") ;
}
}

-------------------------- 邪恶的分割线 ----------------------------
问题:
       我发现,1秒后, 程序照样没运行main函数这个线程!!!不是说sleep(1000)休息 >=1秒 么, 就算线程之间交换时间片,也不至于一直要MyThread1运行结束才main吧 ???

望指教 !!!
------解决方案--------------------
应该是输出量太大,你没能看到main方法的输出
把throw new RuntimeException("-----------------") ;改成System.exit(0);效果就明显了
  相关解决方案