以下代码导致java.lang.IllegalThreadStateException: Thread already started我在程序中第二次调用start()方法时.
updateUI.join();
if (!updateUI.isAlive())
updateUI.start();
Run Code Online (Sandbox Code Playgroud)
第二次updateUI.start()调用就会发生这种情况.我已经多次执行它并且调用线程并且在击中之前完成运行完成updateUI.start().
调用updateUI.run()避免了错误,但导致线程在UI线程(调用线程,如SO上的其他帖子中提到)中运行,这不是我想要的.
线程只能启动一次吗?如果我想再次运行该线程怎么办?这个特定的线程在后台进行一些计算,如果我不在线程中执行它而不是在UI线程中完成,并且用户有一个不合理的漫长等待.
我想向新手解释线程死锁.我见过许多死锁的例子,有些使用代码,有些使用插图(比如着名的4辆汽车).还有像The Dining Philosophers这样经典的容易陷入僵局的问题,但这些问题可能过于复杂,无法让真正的新手完全掌握.
我正在寻找最简单的代码示例来说明死锁是什么.这个例子应该:
您有什么推荐的吗?
我正在寻找一个可以提供超时的ExecutorService实现.提交给ExecutorService的任务如果花费的时间超过运行超时,则会中断.实现这样的野兽不是一项艰巨的任务,但我想知道是否有人知道现有的实施.
以下是我根据下面的一些讨论提出的内容.任何意见?
import java.util.List;
import java.util.concurrent.*;
public class TimeoutThreadPoolExecutor extends ThreadPoolExecutor {
private final long timeout;
private final TimeUnit timeoutUnit;
private final ScheduledExecutorService timeoutExecutor = Executors.newSingleThreadScheduledExecutor();
private final ConcurrentMap<Runnable, ScheduledFuture> runningTasks = new ConcurrentHashMap<Runnable, ScheduledFuture>();
public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, long timeout, TimeUnit timeoutUnit) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
this.timeout = timeout;
this.timeoutUnit = timeoutUnit;
}
public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, long timeout, …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个静态方法,可以同时从多个线程调用.我的数据是否存在混淆的危险?
在我的第一次尝试中,该方法不是静态的,我正在创建该类的多个实例.在那种情况下,我的数据以某种方式混淆了.我不确定这是怎么发生的,因为它有时只会发生.我还在调试.但是现在这个方法是静态的,到目前为止我没有问题.也许这只是运气.我不确定.
我在Android上尝试一些新东西,我需要访问UI线程的处理程序.
我知道以下内容:
我希望我的服务必须获取UI线程处理程序并将消息放入此处理程序.这样就可以处理此消息并将其发布到UI.这里的服务将是一个正常的服务,将由一些应用程序启动.
我想知道这是否可行.如果是这样,请建议一些代码片段,以便我可以尝试.
关心Girish
嗨,我正在尝试使用std::threadG ++.这是我的测试代码
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
Run Code Online (Sandbox Code Playgroud)
它编译,但当我尝试运行它时,结果是:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
Run Code Online (Sandbox Code Playgroud)
我的编译器版本:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
我的测试代码出了什么问题?
更新:我使用以下命令行来编译和运行我的代码.
$ g++ -std=c++0x test.cpp
$ ./a.out
Run Code Online (Sandbox Code Playgroud)
我试过 …
有没有办法创建一次运行多个方法的线程.如果任何方法失败,则应该杀死所有其他线程.
提前致谢
System.Timers.Timer是否在与创建它的线程不同的线程上经过?
让我们说我有一个定时器,每5秒触发一次.当计时器触发时,在已用方法中,某些对象被修改.让我们说修改这个对象需要很长时间,比如10秒.在这种情况下,我是否可能遇到线程冲突?
对于线程与线程相比,Actors的工作方式是否有任何好的和简短的解释?
一个线程不能被视为一个actor并将消息发送到其他线程吗?我看到了一些不同,但对我来说并不是那么清楚.我可以通过不同的线程使用任何语言的Actors吗?
我有一个方法HandlerThread.一个值在内部发生变化Thread,我想将它返回给test()方法.有没有办法做到这一点?
public void test()
{
Thread uiThread = new HandlerThread("UIHandler"){
public synchronized void run(){
int value;
value = 2; //To be returned to test()
}
};
uiThread.start();
}
Run Code Online (Sandbox Code Playgroud)