Th()方法使运行它的线程进入Hibernate 状态. 下面代码中的thread.sleep()语句将由主线程运行,下面代码中的‘线程’是用户定义的线程.为什么在调用用户定义的线程的引用时主线程处于Hibernate 状态.

public class ThreadDemo {

    public static Thread t1 = null;
    public static Thread t2 = null;
    public static void main(String[] args) throws InterruptedException{

        System.out.println("start of main function, Thread : "+Thread.currentThread().getName());
        t1 = Thread.currentThread(); //main thread
        MyThread thread = new MyThread();
        t2 = thread;
        thread.start();

        System.out.println("Before sleep in main method : "+System.currentTimeMillis());
        thread.sleep(1000);
        System.out.println("After sleep in main method : "+System.currentTimeMillis());

        System.out.println("In main method, Thread : "+t1.getName()+" ,In state: "+t1.getState());
        System.out.println("In main method, Thread : "+t2.getName()+" ,In state: "+t2.getState());

        System.out.println("Is thread, "+thread.getName() + " alive : "+ thread.isAlive());

        System.out.println("end of main function, Thread : "+Thread.currentThread().getName());
    }

    public static class MyThread extends Thread{
        int sum = 0;
        @Override
        public void run() {
            System.out.println("In run method, Thread : "+t1.getName()+" ,In state: "+t1.getState());
            System.out.println("In run method, Thread : "+t2.getName()+" ,In state: "+t2.getState());
            System.out.println("start of run method, Thread : "+Thread.currentThread().getName());
            for(int i=0; i<10000; i++){
                sum += i;
            }
            try{
                Thread.sleep(10000);
                System.out.println("After sleep in run method : "+System.currentTimeMillis());
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("end of run method, "+ Thread.currentThread().getName() +", returns : "+sum);
        }
    }
}

输出:

start of main function, Thread : main
Before sleep in main method : 1701105524066
In run method, Thread : main ,In state: TIMED_WAITING
In run method, Thread : Thread-0 ,In state: RUNNABLE
start of run method, Thread : Thread-0
After sleep in main method : 1701105525070
In main method, Thread : main ,In state: RUNNABLE
In main method, Thread : Thread-0 ,In state: TIMED_WAITING
Is thread, Thread-0 alive : true
end of main function, Thread : main
After sleep in run method : 1701105534071
end of run method, Thread-0, returns : 49995000

我不明白在用户定义的线程的引用上调用主线程时,主线程是如何Hibernate 的.

推荐答案

允许一个线程使另一个线程进入Hibernate 状态是一种安全风险,因此是不允许的.睡眠是使当前线程进入睡眠状态的静态方法.

静态方法可以在实例上调用.在哪个实例上调用它无关紧要.

Java相关问答推荐

PostgreSQL货币兑换率查询

在FML中删除关键帧动画

我可以在regex中的字符类中放置断言吗?

具有默认分支的JUnit代码覆盖率切换声明

Java 21虚拟线程会解决转向react 式单线程框架的主要原因吗?

CSS应用于一个端点,但不应用于Thymeleaf中的另一个端点

如何调用Firebase Realtime Database中的子图像列表到android studio中的回收器视图?

如何以干净的方式访问深度嵌套的对象S属性?

Java 21 struct 化连接货币,需要可预知的子任务异常排序

如何从错误通道回复网关,使其不会挂起

如何修复PDF重建过程中的文本定位

Spring Data JPA慢慢地创建了太多非活动会话

Tinylog中的滚动文件会在每次应用启动时覆盖日志(log)文件

Lombok@Nonnull是否也对供应商有影响?

如何在Java springboot中从一个端点发送多个时间响应?

如何在Selenium上继续使用最新的WebDriver版本

javax.crypto-密码对象-提供者服务是如何工作的?

Java泛型方法重载

使用DynamoDB增强客户端时未更新属性

如何在 Android Studio 中删除 ImageView 和屏幕/父级边缘之间的额外空间?