Java 线程是在用户空间还是在内核空间中创建的?
2022-09-04 07:02:06
查看此代码
Thread t1 = new Thread(new Runnable() {
@Override
public void run()
{
try
{
System.out.println("STARTING SERVER...");
ServerSocket s = new ServerSocket(2544);
System.out.println("SERVER BLOCKED ON ACCEPT");
Socket ss = s.accept();
System.out.println("SERVER NOT BLOCKED ANYMORE");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
System.out.println("Hello");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
t2.start();
输出:
STARTING SERVER...
SERVER BLOCKED ON ACCEPT
Hello
Hello
Hello
Hello
Hello
Hello
Hello
...
Java线程应该是用户空间线程,对吧?因此,一个阻塞的线程应该阻塞整个过程...事实并非如此。他们发生了什么?