How to determine if a Java Android Looper is busy - java

I have a simple handling of tasks on a separate thread.
class MyHandlerThread extends Thread {
Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler();
Looper.loop();
}
}
How can I determine if the looper is currently busy handling messages or its message queue is empty and it is waiting for messages?I want to do that from another thread.
Basically, I want to know when the handler is done with a task that I have just submitted.

You can use synchronized method to determine if thread is busy or not
Example:
public class MsLunch {
private long c1 = 0;
private long c2 = 0;
private Object lock1 = new Object();
private Object lock2 = new Object();
public void inc1() {
synchronized(lock1) {
c1++;
}
}
public void inc2() {
synchronized(lock2) {
c2++;
}
}
}
For More: http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

Related

How to ensure synchronization when one thread keeps sending data to another thread?

There are 2 threads: Thread1 and Thread2, in addition to the main UI thread.
The UI thread spawns Thread1. Thread1 spwans Thread2 and keeps sending an array to Thread2 at periodic intervals.
Thread2 should keep listening to Thread1 and whenever it receives data, Thread2 will process it and send the result back to the UI thread.
How can this be achieved? I started off using handlers but did not know how to send Thread2's handler to Thread1, so that Thread1 can keep posting data to Thread2's message queue.
This is Thread1:
public class Thread1 extends Thread {
Handler handler;
int a[] = {1,2,3,4,5,6,7,8,9,10};
#Override
public void run() {
Thread2 thread2 = new Thread2();
thread2.start();
handler = thread2.getHandler();
for(int i = 0; i < 5; i++){
thread2.passData(a);
handler.sendEmptyMessage(0);
try{
Thread.sleep(1000);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
}
This is Thread2:
public class Thread2 extends Thread {
private int a[];
Handler handler;
public void passData(int a[]) {
this.a = new int[10];
this.a = a.clone();
}
public Handler getHandler() {
return handler;
}
#Override
public void run() {
if(Looper.myLooper() == null) {
Looper.prepare();
}
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
for(int i = 0; i < a.length; i++) {
//random work
}
}
};
Looper.loop();
}
}
I don't know if you need a good performance but one simple way to achieve is to use this object
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html
Assuming Thread1 and Thread2 are a both HandlerThreads, they can can just send messages to each other using each other's Handler(s).
I started off using handlers but did not know how to send Thread2's handler to Thread1, so that Thread1 can keep posting data to Thread2's message queue.
Specific to this question, you can either create a method on HandlerThread2 that wraps the handler.post() and call that from HandlerThread1's reference to HandlerThread2.
//in HandlerThread2
public void send(SomeData data) {
this.handler.post([runnable]);
}
Or you can provide an accessor in HandlerThread2 to get the actual handler from HandlerThread2 and use it directly.
//in HandlerThread2
public Handler getHandler() {
return this.handler;
}

Deadlock when calling two synchronized method

class Downloader extends Thread {
private InputStream in;
private OutputStream out;
private ArrayList<ProgressListener> listeners;
public Downloader(URL url, String outputFilename) throws IOException {
in = url.openConnection().getInputStream();
out = new FileOutputStream(outputFilename);
listeners = new ArrayList<ProgressListener>();
}
public synchronized void addListener(ProgressListener listener) {
listeners.add(listener);
}
public synchronized void removeListener(ProgressListener listener) {
listeners.remove(listener);
}
private synchronized void updateProgress(int n) {
for (ProgressListener listener: listeners)
listener.onProgress(n);
}
public void run() {
int n = 0, total = 0;
byte[] buffer = new byte[1024];
try {
while((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
total += n;
updateProgress(total);
}
out.flush();
} catch (IOException e) { }
}
}
The above code is from the book "seven concurrency models in seven weeks". The book says the above code is having potential for the deadlock as the the synchronized method updateProgress calls a alien method[onProgress] that might acquire another lock.
Since we acquire two locks without right order, the deadlock might occur.
Can anyone explain how the deadlock happens in the above scenario?
Thanks in advance.
It's best to make the objects you use with synchronized private.
Since you synchronize on the Downloader, you don't know whether other threads synchronize on the Downloader too.
The following listener causes a deadlock:
MyProgressListener extends ProgressListener {
public Downloader downloader;
public void onProgress(int n) {
Thread t = new Thread() {
#Override
public void run() {
synchronized(downloader) {
// do something ...
}
}
};
t.start();
t.join();
}
}
Code that deadlocks:
Downloader d = new Downloader(...);
MyProgressListener l = new MyProgressListener();
l.downloader = d;
d.addListener(l);
d.run();
The following will happen if you run that code:
the main thread reaches the updateProgress and aquires a lock on the Downloader
the MyProgressListener's onProgress method is called and the new thread t is started
the main thread reaches t.join();
In this situation the main thread cannot procede until t is finished, but for t to finish, the main thread would have to release it's lock on the Downloader, but that won't happen since the main thread can't procede -> Deadlock
First off, recall that the keyword synchronized, when applied to a a class, implies locking the whole object this method belongs to. Now, let's sketch out another couple of objects triggering the deadlock:
class DLlistener implements ProgressListener {
private Downloader d;
public DLlistener(Downloader d){
this.d = d;
// here we innocently register ourself to the downloader: this method is synchronized
d.addListener(this);
}
public void onProgress(int n){
// this method is invoked from a synchronized call in Downloader
// all we have to do to create a dead lock is to call another synchronized method of that same object from a different thread *while holding the lock*
DLthread thread = new DLThread(d);
thread.start();
thread.join();
}
}
// this is the other thread which will produce the deadlock
class DLThread extends Thread {
Downloader locked;
DLThread(Downloader d){
locked = d;
}
public void run(){
// here we create a new listener, which will register itself and generate the dead lock
DLlistener listener(locked);
// ...
}
}
One way to avoid the dead lock is to postpone the work done in addListener by having internal queues of listeners waiting to be added/removed, and have Downloader taking care of those by itself periodically. This ultimately depends on Downloader.run inner working of course.
Probably the problem in this code:
for (ProgressListener listener: listeners)
listener.onProgress(n);
When one thread, which holds a lock, calls an external method
like this one (onProgress) then you cannot guarantee that
implementation of this method won't try to obtain other lock,
which could be held by different thread. This may cause a deadlock.
Here's a classic example that shows the kind of hard-to-debug problems the author is trying to avoid.
The class UseDownloader is created and downloadSomething is called.
As the download progresses, the onProgress method is called. Since this is called from within the synchronized block, the Downloader motinor is locked. Inside our onProgress method, we need to lock our own resource, in this case lock. So when we are trying to synchronize on lock we are holding the Downloader monitor.
If another thread has decided that the download should be canceled, it will call setCanceled. This first tests done so it synchronized on the lock monitor and then calls removeListener. But removeListener requires the Downloader lock.
This kind of deadlock can be hard to find because it doesn't happen very often.
public static final int END_DOWNLOAD = 100;
class UseDownloader implements ProgressListener {
Downloader d;
Object lock = new Object();
boolean done = false;
public UseDownloader(Downloader d) {
this.d = d;
}
public void onProgress(int n) {
synchronized(lock) {
if (!done) {
// show some progress
}
}
}
public void downloadSomething() {
d.addListener(this);
d.start();
}
public boolean setCanceled() {
synchronized(lock) {
if (!done) {
done = true;
d.removeListener(this);
}
}
}
}
The following example leads to a deadlock because the MyProgressListener tries to acquire the Downloader lock while it's already acquired.
class MyProgressListener extends ProgressListener {
private Downloader myDownloader;
public MyProgressListener(Downloader downloader) {
myDownloader = downloader;
}
public void onProgress(int n) {
// starts and waits for a thread that accesses myDownloader
}
}
Downloader downloader = new Downloader(...);
downloader.addListener(new MyListener(downloader));
downloader.run();

Get ThreadID of a Thread before it starts

I have this pairing mechanism idea that identifies Clients by their ThreadID. When the thread is destroyed, the client's pairing is destroyed and its ID is set to -1.
Now in order for me to get the Thread ID, the thread must of course have been running or started.
I want to know if I can create a thread, get the ID and then set the runnable later on OR if I can get the ID of the current thread that's running my function..
Example of what I want to do:
void createClientThreaded(final Client client) {
new Thread(new Runnable() {
#Override public void run() {
while(!client.stop()) {
Utils.sleep(1000);
//Do other stuff here..
client.setThreadID(/* This Thread's ID */);
// Do other stuff here..
}
}
});
}
Any ideas on how I can do this? Also would the Thread ID's be unique per process or unique for the whole system? In other words, can two jar files running at the same time have the same Thread ID?
The thread id is immutable, so you won't be able to set it to -1 when the thread terminates.
The id is created as soon as the thread is created, not as soon as the thread is started, so you can create the thread, read its id, and start it later.
However, if you're creating and destroying a lot of threads, then you're going to incur a high runtime cost. It would be more efficient to use a ThreadPoolExecutor to execute your runnables, however this precludes the option of creating a thread, reading its id, and starting the thread later. Another option is to create your own class that implements Runnable with a mutable id.
class MyRunnable implements Runnable {
private static AtomicLong idCounter = new AtomicLong(0);
private long id;
public MyRunnable() { this.id = idCounter.getAndIncrement(); }
public void setId(long id) { this.id = id; }
public long getId() { return this.id; }
public void run() {}
}
Thread ids are per process, not system-wide. In addition, the process may reuse thread ids (e.g. if the thread with id=5 terminates, then the process may assign id=5 to a new thread).
To run a runnable at some later time:
import java.util.concurrent.Semaphore;
public class Main {
public static void main(String[] args) {
DelayedExecutionThread blocker = new DelayedExecutionThread();
Thread t = blocker.getThread();
t.start();
System.out.println("Thread Started..");
sleep(3000);
System.out.println("Executing..");
blocker.setRunnable(new Runnable() {
#Override
public void run() {
System.out.println("RAN THE THREAD LATER");
}
});
sleep(1);
System.out.println("Executed..");
}
private static class DelayedExecutionThread {
private Semaphore lock = new Semaphore(1, true);
private Thread thread;
private Runnable target;
public DelayedExecutionThread() {
this.lock.acquireUninterruptibly();
this.thread = new Thread(new Runnable() {
#Override
public void run() {
DelayedExecutionThread.this.lock.acquireUninterruptibly();
DelayedExecutionThread.this.lock.release();
DelayedExecutionThread.this.lock = null;
if (DelayedExecutionThread.this.target != null) {
DelayedExecutionThread.this.target.run();
}
}
});
}
public Thread getThread() {
return this.thread;
}
public void setRunnable(Runnable runnable) {
this.lock.release();
this.target = runnable;
}
}
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I used a semaphore. You just need some sort of locking or condition. When the thread is started, it attempts to acquire the lock which causes it to "wait" until it can acquire it. When you set the runnable at some later point in time, it will release the lock causing the thread to acquire it, release it, and continue execution.

How to check if a thread is running in the ExecutorService Thread pool

How do i check if a thread is running in the pool of thread ExecutorService?
Background:
I want to synchronize between the threads in the Thread pool if there is a flag set.
So if the flag is set to true for synchronization, then I have to check if other Threads are running or wait for its completion and then invoke the blocking thread with synchronize, so that other threads would wait for this blocking thread to finish.
If flag is not set then no need to synchronize and could execute the threads in parallel.
Thanks!
You need to use a Semaphore.
This allows you to have a number of "permits" to do work. If you only want one task running at a time then have a Semaphore with one permit, otherwise have a Semaphore with a number of permits greater than the number of Threads in the pool.
static class Worker implements Runnable {
final Semaphore semaphore;
public Worker(Semaphore semaphore) {
this.semaphore = semaphore;
}
#Override
public void run() {
try {
semaphore.acquire();
try {
//do stuff
} finally {
semaphore.release();
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
public static void main(String[] args) {
final int numThreads = 10;
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final Semaphore semaphore;
boolean myflag = true;
if (myflag) {
semaphore = new Semaphore(1);
} else {
semaphore = new Semaphore(numThreads);
}
final Worker worker = new Worker(semaphore);
executorService.submit(worker);
}
This example is a little contrived as you can just use a newSingleThreadExecutor() when you only need one task to run at a time - but I assume you know that and for some reason cannot.
EDIT
Having poked around a little to see if this can be tidied I came across this. This hints at a neater solution:
static interface TaskBlocker {
void acquire();
void release();
}
static class Worker implements Runnable {
final TaskBlocker taskBlocker;
public Worker(TaskBlocker taskBlocker) {
this.taskBlocker = taskBlocker;
}
#Override
public void run() {
taskBlocker.acquire();
try {
//do stuff
} finally {
taskBlocker.release();
}
}
}
public static void main(String[] args) {
final int numThreads = 10;
final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
final TaskBlocker taskBlocker;
boolean myflag = true;
if (myflag) {
taskBlocker = new TaskBlocker() {
final Lock lock = new ReentrantLock();
#Override
public void acquire() {
lock.lock();
}
#Override
public void release() {
lock.unlock();
}
};
} else {
taskBlocker = new TaskBlocker() {
#Override
public void acquire() {
}
#Override
public void release() {
}
};
}
final Worker worker = new Worker(taskBlocker);
executorService.submit(worker);
}
In short, you don't. Executors are not meant to be used that way. If you want to manage your threads manually, do it without Executors. If you use Executors, shift your thinking from Threads to Runnables. Make your Runnables or the Classes and methods thread-safe, using synchronization or any of the high-level abstractions in java.util.concurrent.
How do i check if a thread is running in the pool of thread ExecutorService?
If you just want to know if a thread is running in a specific ExecutorService, you can create the ExecutorService with a specific ThreadFactory and have it attach some special property to the thread, such as a special name.
private static final String EXECUTOR_THREADNAME_PREFIX = "ExecutorThread";
ThreadFactory threadFactory = new ThreadFactory() {
private final AtomicInteger id = new AtomicInteger(0);
#Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName(EXECUTOR_THREADNAME_PREFIX + "_" + id.incrementAndGet());
return thread;
}
};
myExecutor = Executors.newCachedThreadPool(threadFactory);
Then, in the thread, you simply check if the name starts with your prefix:
if (Thread.currentThread().getName().startsWith(EXECUTOR_THREADNAME_PREFIX)) {
// In executor.
} else {
// Not in executor.
}

How to notify all observers without holding the thread?

I have a thread inside a class like this-
import java.util.Observable;
public class Download extends Observable {
private int state = 0;
private final Thread myThread = new Thread(() -> {
/*
some work to do here
*/
setChanged();
notifyObservers(state);
});
public void download(int state) {
if (!myThread.isAlive()) {
this.state = state;
myThread.start();
}
}
public Thread getThread() {
return myThread;
}
public static void MyMethod() throws InterruptedException {
Download down = new Download();
down.addObserver((Observable ob, Object dat) -> {
System.out.println(ob);
if ((int) dat == 1) {
down.download(2);
} else {
System.out.println("success");
}
});
down.download(1);
down.getThread().join();
}
public static void main() throws InterruptedException {
MyMethod();
}
}
The problem is I never get it to print the "success" message.
I assume, it is because all observers are being notified from inside of MyThread. So when down.download(2) is called from the observer inside MyMethod(), the previous thread is still running and the call is ignored.
How can I notify all observers from the main thread, not from the myThread?
You are calling down.download(2) from within the execution of MyThread, therefore the thread is still alive which means that your download method does nothing because of if(!myThread.isAlive()).
I would recommend you to use the Executor framework and Listenable Futures from Guava instead of creating threads manually. Example code from the Guava wiki:
ListeningExecutorService service =
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
ListenableFuture<Explosion> explosion = service.submit(new Callable<Explosion>() {
public Explosion call() {
return pushBigRedButton();
}
});
Futures.addCallback(explosion, new FutureCallback<Explosion>() {
// we want this handler to run immediately after we push the big red button!
public void onSuccess(Explosion explosion) {
walkAwayFrom(explosion);
}
public void onFailure(Throwable thrown) {
battleArchNemesis(); // escaped the explosion!
}
});
Note that Futures.addCallback(..) also has an overload which allows you to determine which executor should execute the callback, this seems to be what you want.

Categories