检查此代码
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 线程应该是用户空间线程,对吧?所以一个被阻塞的线程应该阻塞整个过程......那不是发生的事情。他们发生了什么事?