Viv*_*mar 1 java multithreading
我有一个代码 -
public class ThreadOne
{
public static void main(String[] args)
{
Thread1 th=new Thread1();
Thread1 th2=new Thread1();
th.start();
th2.start();
System.exit(1);
}
}
class Thread1 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道的是 -
System.exit(1);将终止当前运行的Java虚拟机.当你的程序退出时,你的线程也会死掉.
Thread是一部分Process,如果Process已经退出,那么所有线程都将被销毁.
Thread.join() 将等待线程运行完成.
public class ThreadOne
{
public static void main(String[] args)
{
Thread1 th=new Thread1();
Thread1 th2=new Thread1();
th.start();
th2.start();
th.join();
th2.join();
System.exit(1);
}
}
class Thread1 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)