gpt4 book ai didi

java - 在线程 hibernate 时执行主类 - java

转载 作者:行者123 更新时间:2023-12-01 10:16:15 25 4
gpt4 key购买 nike

有人可以帮助我了解线程在 java 中的工作原理吗?

我有一个主课:

public static void main(String[] args) {
Runnable r = new TestThread();
new Thread(r).start();
executor.execute(r);
System.out.println("Hey the thread has ended")
}

和线程类:

public class TestThread implements Runnable {
public class TestThread() {
sleep(20000);
}

@Override
public void run() {
// TODO Auto-generated method stub
}

}

如何获得短语:“嘿,线程已结束”,而不必在线程 hibernate 时等待?

最佳答案

你可以这样做:调用线程类的start方法,并在Run实现中让线程 hibernate ...

请注意,您的应用将在线程从 sleep 状态返回并继续之前终止

Runnable 类

    public class TestThread implements Runnable {

@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void setParameter(String string) {
// TODO Auto-generated method stub

}
}

实现

 public static void main(String[] args) {
TestThread r = new TestThread();
r.setParameter("myParameterHere");
Thread t = new Thread(r);
t.setName("asdas");
t.start();
System.out.println("Hey the thread has ended");
}

关于java - 在线程 hibernate 时执行主类 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com