Calling main thread from Runnable thread in java

I have implemented the Runnable interface for loading fragments of an image, and I want to call the Main thread from this secondary stream to display fragments. can someone tell me how to call the main thread from the Runnable Interface thread in Java.

thanks.

+6
java multithreading
source share
4 answers

Instead of Runnable you can use Callable<Set<Image>> , which returns a set of loaded images. Send this called task to the executor, get Future<Set<Image>> and wait for the download thread to complete its task.

For example:

 Future<Set<Image>> future = Executors.newSingleThreadExecutor().submit(new Callable<Set<Image>>() { @Override public Set<Image> call() throws Exception { return someServiceThatLoadsImages.load(); } }); try { Set<Image> images = future.get(); display(images); } catch (Exception e) { logger.error("Something bad happened", e); } 
+4
source share

What do you mean by "call a thread"?

You probably want to somehow โ€œnotifyโ€ the main thread that the images have been uploaded. You could do this, for example, so that the main wait() stream for the boot stream is notify() , so that the images are loaded.

Preferably, however, you should use one of the higher-level concurrency classes in the java.util.concurrent package. For example, Callable and Future , as suggested by Boris Pavlovich. There is an example using Future documentation .

0
source share

You cannot โ€œnameโ€ one thread from another. What you can do is have a message queue: the secondary thread puts the messages in the queue, and the main thread will pick them up from the queue and process them.

0
source share

I think that the "call thread" the author had in mind some kind of callback from the workflow to the main thread to notify him of some event.

There are many ways to implement such a callback, but I would use a simple listener interface. Something like that:

 //Interface of the listener that listens to image load events public interface ImageLoadListener { void onImageLoadSuccess(ImageInformation image); void onImageLoadFailed(ImageInformation image); } //Worker thread that loads images and notifies the listener about success public class ImageLoader implements Runnable { private final ImageLoadListener loadListener; public ImageLoader(ImageLoadListener listener) { this.loadListener = listener; } public void run() { .... for (each image to load) { .... if (load(image)) { if (loadListener != null) { loadListener.onImageLoadSuccess(image); } } else { if (loadListener != null) { loadListener.onImageLoadFailed(image); } } } } } //Main class that creates working threads and processes loaded images public class MainClass { ... //Main method for processing loaded image void showImageAfterLoad(ImageInformation information) { ... } //Some button that should create the worker thread void onSomeButtonClick() { //Instantiate the listener interface. If image load is successful - run showImageAfterLoad function on MainClass ImageLoadListener listener = new ImageLoadListener() { void onImageLoadSuccess(ImageInformation image) { MainClass.this.showImageAfterLoad(image); } void onImageLoadFailed(ImageInformation image) { //Do nothing } }; //Create loader and it thread ImageLoader loader = new ImageLoader(listener); new Thread(loader).start(); } } 

If you need it to be scalable, I would recommend changing it to some implementation of the Bus Bus template. For example: http://code.google.com/p/simpleeventbus/

The event bus is usually the same as I wrote in my example, but it is very scalable. The main difference is that you can manage different types of events and register many listeners at once.

-one
source share

All Articles