Gene
Gene

Reputation: 2218

Invoke runnable inside a method and pass object

I am trying to invoke a runnable thread which puts item into a LinkedBlockingList. The problem I am facing now is that I am unable to run the inner methods. Only the outer method is running. I know I have to add lines of code below at the comments but I am really clueless.

public void prod(TwitterObj obj) {
System.out.println("Prod Outer");
class Producer implements Runnable {

    TwitterObj obj;
    @Override
    public void run() {
        try {
            queue.put(obj);
            System.out.println("Put OBJ into queue: " + obj.getMsg());
            ++i;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };

    //invoke thread
}}

"Prod Outer" message is being displayed but the inner class is not running at all. Any feedback would be greatly appreciated!

EDIT 1:

This is my code which i listen to my own twitter stream and add the values into an object

    public void onStatus(twitter4j.Status status) {
                System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText() + " - " + status.getCreatedAt());

                obj.setMsg(status.getText());
                obj.setDateTime(String.valueOf(status.getCreatedAt()));
                obj.setId((int) status.getId());
                prod(obj);
            }

Upvotes: 0

Views: 62

Answers (1)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

If you like to know how to pass the TwitterObj to your Runnable here is how to do that.

class Producer implements Runnable {
    private TwitterObj obj;

    public Producer(TwitterObj obj) {
        this.obj = obj;
    }

    @Override
    public void run() {
        try {
            queue.put(obj);
            System.out.println("Put OBJ into queue: " + obj.getMsg());
            ++i;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
}

Now you can create a Producer as follow:

TwitterObj twitterObj = ....;

Producer producer = new Producer(twitterObj);
Thread t = new Thread(producer);
t.start();

In particular replace

public void onStatus(twitter4j.Status status) {
    System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText() + " - " + status.getCreatedAt());

    obj.setMsg(status.getText());
    obj.setDateTime(String.valueOf(status.getCreatedAt()));
    obj.setId((int) status.getId());
    prod(obj);
}

with

public void onStatus(twitter4j.Status status) {
    System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText() + " - " + status.getCreatedAt());

    obj.setMsg(status.getText());
    obj.setDateTime(String.valueOf(status.getCreatedAt()));
    obj.setId((int) status.getId());

    // prod(obj);  removed and replaced with the 3 lines below
    Producer producer = new Producer(twitterObj);
    Thread t = new Thread(producer);
    t.start();
}

Upvotes: 1

Related Questions