为什么在类中声明wait()和notify()方法Object而不是Thread类?
只想就多线程任务的"最佳实践"提出一些建议.
例如,我们有一个C#应用程序,它在启动时从我们数据库中的各种"类型"表中读取数据,并将信息存储在我们传递给应用程序的集合中.这可以防止我们每次需要此信息时都会访问数据库.
此时应用程序正在同步读取10个表中的数据.我真的希望从一个并行运行的不同线程中的每个表中读取应用程序.在继续启动应用程序之前,应用程序将等待所有线程完成.
我已经研究过BackGroundWorker但只是想要一些关于完成上述工作的建议.
我期待着一些答案
我试图使用ThreadPoolExecutor执行许多任务.以下是一个假设的例子:
def workQueue = new ArrayBlockingQueue<Runnable>(3, false)
def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue)
for(int i = 0; i < 100000; i++)
threadPoolExecutor.execute(runnable)
Run Code Online (Sandbox Code Playgroud)
问题是我很快得到了java.util.concurrent.RejectedExecutionException,因为任务数量超过了工作队列的大小.但是,我正在寻找的所需行为是让主线程阻塞,直到队列中有空间.完成此任务的最佳方法是什么?
我想从更新Progressbar的Thread更新我的UI.不幸的是,当从"runnable"更新进度条的drawable时,进度条消失了!改变进度onCreate()栏的绘图可用于其他工作!
有什么建议?
public void onCreate(Bundle savedInstanceState) {
res = getResources();
super.onCreate(savedInstanceState);
setContentView(R.layout.gameone);
pB.setProgressDrawable(getResources().getDrawable(R.drawable.green)); //**Works**/
handler.postDelayed(runnable, 1);
}
private Runnable runnable = new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run()
{
//* The Complete ProgressBar does not appear**/
pB.setProgressDrawable(getResources().getDrawable(R.drawable.green));
}
});
}
}
Run Code Online (Sandbox Code Playgroud) 是否有可能让一个线程写入OutputStreamJava Socket,而另一个InputStream线程从套接字读取,而线程不必在套接字上同步?
好吧,我已经搜索了很多解决方案.我正在寻找一种干净简单的方法来防止在我停止后调用System.Threading.Timer的回调方法.
我似乎无法找到任何东西,这导致我,在偶尔,诉诸可怕的线程 - thread.sleep-thread.abort组合颤抖.
可以用锁来完成吗?请帮我找到一个好办法.谢谢
我使用ScheduledExecutorService定期执行方法.
p代码:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> handle =
scheduler.scheduleWithFixedDelay(new Runnable() {
public void run() {
//Do business logic, may Exception occurs
}
}, 1, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
我的问题:
如果run()抛出异常,如何继续调度程序?我应该尝试捕获方法中的所有异常run()吗?或者任何内置的回调方法来处理异常?谢谢!
我正在学习用来ExectorService汇集threads和发送任务.我有一个简单的程序如下
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class Processor implements Runnable {
private int id;
public Processor(int id) {
this.id = id;
}
public void run() {
System.out.println("Starting: " + id);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("sorry, being interupted, good bye!");
System.out.println("Interrupted "+Thread.currentThread().getName());
e.printStackTrace();
}
System.out.println("Completed: " + id);
}
}
public class ExecutorExample {
public static void main(String[] args) {
Boolean isCompleted=false;
ExecutorService executor = Executors.newFixedThreadPool(2);
for(int i=0; i<5; i++) {
executor.execute(new …Run Code Online (Sandbox Code Playgroud) 在最长的时间里,我认为异步是在后台线程上运行某些东西的同义词,而同步意味着在主线程上(阻止UI更新和交互).我明白,不在主线程上运行昂贵的操作是因为它不允许在主线程被占用时发生UI操作,但为什么同步麻烦?
但是,我注意到你可以在主线程上进行异步调用,在后台线程上进行同步调用.
我总是听到人们说不要同步或在主线程上使用昂贵的调用,因为它会阻止用户的UI.这两个不同的问题我应该确保我不这样做吗?有什么区别?
multithreading asynchronous objective-c grand-central-dispatch ios
在以下代码中:
class A {
private int number;
public void a() {
number = 5;
}
public void b() {
while(number == 0) {
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果调用方法b然后启动一个触发方法a的新线程,则方法b不能保证看到更改,number因此b可能永远不会终止.
当然,我们可以number volatile解决这个问题.但是出于学术原因,我们假设这volatile不是一个选择:
该JSR-133的常见问题告诉我们:
在我们退出synchronized块之后,我们释放了监视器,它具有将缓存刷新到主内存的效果,因此该线程所做的写操作对其他线程是可见的.在我们进入同步块之前,我们获取监视器,它具有使本地处理器高速缓存无效的效果,以便从主存储器重新加载变量.
这听起来像我只需要两个a并b进入和退出任何synchronized-Block,无论他们使用什么显示器.更确切地说,它听起来像这样......:
class A {
private int number;
public void a() {
number = 5;
synchronized(new Object()) {}
}
public void b() {
while(number == 0) {
// …Run Code Online (Sandbox Code Playgroud) multithreading ×10
java ×6
c# ×2
android ×1
asynchronous ×1
concurrency ×1
executor ×1
ios ×1
notify ×1
objective-c ×1
scheduling ×1
sockets ×1
threadpool ×1
timer ×1
wait ×1