0

I have a callback function in which i receive a string. This string is to be passed to a separate thread for processing since the processing takes time. Also, since multiple callbacks can come simultaneously, I would like to have a synchronized lock till i pass the string into the new thread. But I do not wish to have the new thread (where processing is going on) to be locked also.

Could someone please help me figure out the design for this?

I have written the following code but in this I think in this no callbacks can be received till the whole processing of the separate thread is also done, thereby defeating the whole purpose of this new thread.

String sLine;
onClick(String line){
synchronized (lock) {
                sLine = line;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                    doProcessing(Sline);    

                }).start(); 
}
}
5
  • 1
    Why do you think you need a synchronized block here?
    – shazin
    Commented Apr 10, 2013 at 12:26
  • I want a synchronized lock here so that between sLine = line; and doProcessing(Sline); if we receive a new callback then sLine value would change and we would just skip the processing of the previous callback
    – Sunny
    Commented Apr 10, 2013 at 12:28
  • You didn't join the new thread, so why do you think it would block processing of other callbacks?
    – Perception
    Commented Apr 10, 2013 at 12:31
  • Correct me if i am wrong. When start() is issued for the new thread, its run() would be called. In run I am doing the procesing that takes long. and since start() is inside the lock, will it not wait till run() is completed?
    – Sunny
    Commented Apr 10, 2013 at 12:38
  • @Sunny What do you mean skip the process of the previous callback do you want to cancel the one currently running or ignore the previous onClick submission and the next doProcessing should be done on the most recent submitted after the current processing is complete
    – John Vint
    Commented Apr 10, 2013 at 13:24

2 Answers 2

1

Also, since multiple callbacks can come simultaneously, I would like to have a synchronized lock till i pass the string into the new thread.

A: I don't think you need to put a lock here. This string is not accessed by multi-thread.

But I do not wish to have the new thread (where processing is going on) to be locked also.

A: As I see nothing was locked here :) I think it could be better if you do something like that:

  • Create an class Runner implement Runnable, this class will do processing
  • Everytime you got callback, use ThreadPoolExecutor to execute this Runner. This help you reuse Thread instance.
  • Note that: These line code doesn't need to synchronized, put synchronized inside processing method if you need.

 

// Implement class Runner

public class Runner implements Runnable {
    private String mLine;

    public Runner(String line) {
        mLine = line;
    }

    @Override
    public void run() {
        process();
    }

    public void process() {
        // Do processing with mLine
        // Put synchronized if you need, it bases on your context
    }
}

// Initialize thread pool

private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 1000, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());

// Execute runner when receiving callback

onClick(String s) {
        Runner runner = new Runner(s);
        executor.execute(runner);
    }
0

Try changing like below

String sLine;
onClick(final String line){
                sLine = line;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                    doProcessing(line);    

                }).start();     
}
2
  • this is the Android onClick() callback so I cant change the signature.
    – Sunny
    Commented Apr 10, 2013 at 12:39
  • You mean you can't mark String line final? Are you sure? Just try It should work.
    – shazin
    Commented Apr 10, 2013 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.