在Java中使用超时调用阻塞方法是否有一种标准的好方法?我希望能够做到:
// call something.blockingMethod();
// if it hasn't come back within 2 seconds, forget it
Run Code Online (Sandbox Code Playgroud)
如果这是有道理的.
谢谢.
假设ThreadLocal变量为不同的线程保存不同的值,是否可以从另一个线程访问一个ThreadLocal变量的值?
即在下面的示例代码中,是否可以在t1中从t2读取TLocWrapper.tlint的值?
public class Example
{
public static void main (String[] args)
{
Tex t1 = new Tex("t1"), t2 = new Tex("t2");
new Thread(t1).start();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{}
new Thread(t2).start();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{}
t1.kill = true;
t2.kill = true;
}
private static class Tex implements Runnable
{
final String name;
Tex (String name)
{
this.name = name;
}
public boolean kill = false;
public void …Run Code Online (Sandbox Code Playgroud) 我在ThreadLocal中传递了一个对象.现在我的当前线程将创建新的Child线程.我希望ThreadLocal中的对象也应继续使用子线程.
有没有办法这样做....?
先感谢您....
我需要为ThreadLocal最终运行特定CompletableFuture.supplyAsync供应商的所有线程提供一个。
从javadoc中,我看到CompletableFuture使用“ ForkJoinPool commonPool()”非常适合我的性能用例。
如何将ThreadLocal(然后删除)转移到运行特定CompletableFuture供应商的所有池线程?
备注:
我看到所有CompletableFuture async完成方法都接受执行程序。我想使用默认值,ForkJoinPool commonPool()但是如果无法实现,我想我必须重写ThreadPoolExecutor并实现beforeExecute吗?