java-threads

Difference between Service, Async Task & Thread?

半世苍凉 提交于 2019-11-26 01:56:23
问题 What is the difference between Service, Async Task & Thread. If i am not wrong all of them are used to do some stuff in background. So, how to decide which to use and when? 回答1: Probably you already read the documentation description about them, I won't repeat them, instead I will try to give answer with my own words, hope they will help you. Service is like an Activity but has no interface. Probably if you want to fetch the weather for example you won't create a blank activity for it, for

How to use wait and notify in Java without IllegalMonitorStateException?

谁都会走 提交于 2019-11-25 22:02:49
问题 I have 2 matrices and I need to multiply them and then print the results of each cell. As soon as one cell is ready I need to print it, but for example I need to print the [0][0] cell before cell [2][0] even if the result of [2][0] is ready first. So I need to print it by order. So my idea is to make the printer thread wait until the multiplyThread notifies it that the correct cell is ready to be printed and then the printerThread will print the cell and go back to waiting and so on.. So I

Difference between wait() and sleep()

天涯浪子 提交于 2019-11-25 21:56:20
问题 What is the difference between a wait() and sleep() in Threads? Is my understanding that a wait() -ing Thread is still in running mode and uses CPU cycles but a sleep() -ing does not consume any CPU cycles correct? Why do we have both wait() and sleep() : how does their implementation vary at a lower level? 回答1: A wait can be "woken up" by another thread calling notify on the monitor which is being waited on whereas a sleep cannot. Also a wait (and notify ) must happen in a block synchronized

“implements Runnable” vs “extends Thread” in Java

你说的曾经没有我的故事 提交于 2019-11-25 21:36:35
问题 From what time I\'ve spent with threads in Java, I\'ve found these two ways to write threads: With implements Runnable : public class MyRunnable implements Runnable { public void run() { //Code } } //Started with a \"new Thread(new MyRunnable()).start()\" call Or, with extends Thread : public class MyThread extends Thread { public MyThread() { super(\"MyThread\"); } public void run() { //Code } } //Started with a \"new MyThread().start()\" call Is there any significant difference in these two