我yield()对Java 中方法的使用有点困惑,特别是在下面的示例代码中.我还读过yield()'用于防止执行线程'.
我的问题是:
我相信下面的代码在使用yield()和不使用时都会产生相同的输出.它是否正确?
事实上,什么是主要用途yield()?
在哪些方面与方法yield()不同?join()interrupt()
代码示例:
public class MyRunnable implements Runnable {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
for(int i=0; i<5; i++) {
System.out.println("Inside main");
}
}
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Inside run");
Thread.yield();
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用和不使用上面的代码我获得相同的输出yield():
Inside main
Inside main
Inside main
Inside main
Inside main
Inside run
Inside run
Inside run
Inside …Run Code Online (Sandbox Code Playgroud) 在Java的上下文中,我在打开GUI窗口时创建一个新线程来读取网络输入,当我关闭窗口时,我想释放套接字资源并立即终止线程.现在我使用setSoTimeout方法,但我不想等待超时异常.有人可以提一些建议吗?谢谢!
final ExecutorService executor = Executors.newFixedThreadPool(1);
final Future<?> future = executor.submit(myRunnable);
executor.shutdown();
if(executor.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.println("task completed");
}else{
System.out.println("Executor is shutdown now");
}
//MyRunnable method is defined as task which I want to execute in a different thread.
Run Code Online (Sandbox Code Playgroud)
这是run执行者类的方法:
public void run() {
try {
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
Run Code Online (Sandbox Code Playgroud)
在这里它等待20第二,但是当我运行代码时它抛出一个异常:
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
Run Code Online (Sandbox Code Playgroud)
我无法关闭并发线程破坏Java Executor class.这是我的代码流程:
MyRunnableexecutor 等待10秒钟完成任务.我有一个简单的java ExecutorService运行一些任务对象(实现Callable).
ExecutorService exec = Executors.newSingleThreadExecutor();
List<CallableTask> tasks = new ArrayList<>();
// ... create some tasks
for (CallableTask task : tasks) {
Future future = exec.submit(task);
result = (String) future.get(timeout, TimeUnit.SECONDS);
// TASKS load some classes and invoke their methods (they may create additional threads)
// ... catch interruptions and timeouts
}
exec.shutdownNow();
Run Code Online (Sandbox Code Playgroud)
在完成所有任务(DONE或TIMEOUT-ed)之后,我尝试关闭执行程序,但它不会停止:exec.isTerminated() = FALSE.
我怀疑某些被执行的任务未正确终止.
是的,我知道执行者的关闭不能保证任何事情:
除尽力尝试停止处理主动执行任务之外,没有任何保证.例如,典型的实现将通过{@link Thread#interrupt}取消,因此任何未能响应中断的任务都可能永远不会终止.
我的问题是,有没有办法确保这些(任务)线程终止?我提出的最佳解决方案是System.exit()在程序结束时调用,但这很简单.
谁能向我解释为什么第一个线程不起作用而第二个线程完美运行:
public class Test {
public static void main(String args[]) throws InterruptedException {
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
t1.startThread();
t2.start();
Thread.sleep(4000);
t1.stopThread();
t2.stopThread();
}
}
class TestThread1 extends Thread {
private volatile TestThread1 thread;
public void startThread() {
thread = new TestThread1();
thread.start();
}
public void run() {
while (thread != null) {
System.out.println("RUNNING 1 ...");
}
}
public void stopThread() {
thread = null;
}
}
class TestThread2 extends Thread {
private volatile boolean …Run Code Online (Sandbox Code Playgroud)