Sandy
Sandy

Reputation: 323

Java Multithreading concepts

Here in my code this setMultiLegNetQuote() method is in one thread and then if you see the highlighted block it is invoking other thread..

My questions are..

(1) This first thread is invoked as below...

Thread t = custStreamerMap.get(mmsg.getCustomerId());
if (null != t) {
            return t.setQuote(mmsg.getMessageBody());
        }

Whether in this case thread run method is invoked as we dint create thred object but we are directly calling t.setQuote(mmsg.getMessageBody());

synchronized int setQuote(String multiLegData) {

                            NetQuoteTuple netQuoteTuple = new NetQuoteTuple();                          
                            if (some checking) { 
                                netQuoteTuple.setSide(1);
                            } else if (some checking) {
                                netQuoteTuple.setSide(2);
                            }
                            netQuoteList.add(netQuoteTuple);                        
                    **dataBufferThread.setNetQuoteList(netQuoteList);
                    dataBufferThread.setCnt(20000);**
                    return 1;
    }

(2) Then dataBufferThread i mean second thread is invoked from first thread. Whether run method of second thread is invoked when we call the thread this way?

dataBufferThread.setNetQuoteList(netQuoteList) ??????????

and then this dataBufferThread.setCnt(20000); does what in case of thread? which is an AtomicInteger declared this way AtomicInteger cnt = new AtomicInteger(0)

Here actually this dataBufferThread and Thread both are seperate threads with run method extending Thread...

That is the reason i asked if we create an object for thread and access some method of it...whether the run method of the thread is invoked?

Upvotes: 1

Views: 186

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533492

I wouldn't use an object which you also describe as a Thread as this is more likely to be confusing than useful.

The only method on a Thread which invokes it is start() From that point it runs independently.

  1. This calls a method on the Thread object. This doesn't do anything to/for that thread and it doesn't even have to be running.

    return t.setQuote(mmsg.getMessageBody());
    
  2. All you are doing is setting two values, nothing more. Involving a thread as well is just confusing IMHO.

Upvotes: 3

Related Questions