Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8518213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:55:09+00:00 2026-06-11T05:55:09+00:00

I am having a code this way, public int handle_refresh(Data mmsg) throws Exception {

  • 0

I am having a code this way,

public int handle_refresh(Data mmsg) throws Exception {
        String custId = mmsg.getCustomerId();       
        CustomerThread t = custMap.get(mmsg.getCustomerId());
        if (t == null || !t.isAlive()) {
            t = (CustomerThread) context.getBean("custT");
            t.initThread(mmsg.getCustomerId(), mmsg.getCustomerId(), mmsg.getMessageBody());
            custSMap.put(mmsg.getCustomerId(), t);
            t.createBufferThread();
            t.start();
            t.initStreaming();
        }
        synchronized (t) {
            if (null != t) {
                ret = t.addSymbols(mmsg);
            }
        }
        return ret;
    }


                        }

Here CustomerThread is checked in custMap,

Map custMap= new CustomerThread ();

if thread is there in custMap

1) then read spring appilcation context and get it. t = (CustomerThread) context.getBean("custT");

2) In initThread method set the name of the thread uniquely for each customer. t.initThread(mmsg.getCustomerId(), mmsg.getCustomerId(), mmsg.getMessageBody());

3) then put the newly created thread in to map custMap. custSMap.put(mmsg.getCustomerId(), t);

4) then in createBufferThread data is setting into cache.. t.createBufferThread();

5) then start the thread newly and then get data from db. t.start();

6) set the db connections

if thread is not there in custMap

1) synchronized (t) .

2) call t.addSymbols() method.

My questions are…

1) Here does the first if block executes only first time and if once thread is created always synchronized (t) is executed?

I mean all the above 1 to 6 steps in if block are executed only once?

2) then what does synchronized (t) does?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:55:11+00:00Added an answer on June 11, 2026 at 5:55 am

    It looks to me that sychronized (t) is protecting the addSymbols method to make it thread-safe. Calls to this method are adding symbols, I assume, to some data structure within the t thread. It may be that other methods in that thread are synchronized which would mean that it would be locking on the Thread instance. That’s what sychronized (t) is doing here.

    But this is an extremely ugly way of adding thread-safety. The addSymbols(...) method should itself lock on a lock object or, if necessary, be a synchronized method. A class should be responsible for it’s own locking and not require the caller to do something.

    Couple other comments about your code:

    t = (CustomerThread) context.getBean("custT");
    

    The above code seems to be getting a thread instance from Spring. This is typically a singleton unless the “custT” bean is some sort of thread-factory. If it is not a factory bean then you are going to be getting the same thread-object for each call to your handle_refresh method and reinitializing it. This is most likely not what you want.

    synchronized (t) {
        if (null != t) {
            ret = t.addSymbols(mmsg);
        }
    }
    

    If t was null then the synchronized line would throw a NPE. You don’t need the null check inside of synchronized.

    CustomerThread t = custMap.get(mmsg.getCustomerId());
    

    If the handle_refresh(...) method is called from multiple threads then you need to make sure that the custMap is properly synchronized as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having this code... var b = new ReadOnlyCollection<int>(new[] { 2, 4, 2, 2 });
I am having the code this way.. 1) Invoking the updatedb method using reflection...
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
Having this code: $main = !empty($searchResults['main']) ? $searchResults['main'] : null; $second = !empty($searchResults['second']) ?
Im having this code %button.aristo-default.accept{:href => #, :onclick => $.post('/sessions/accept_invite', { user_id: 1, friend_id:
Hey im having this code so far - (id)initWithDictionary:(NSDictionary *)aDictionary { self = [super
Basically i am having problems with my code - this is homework so would
I'm having a problem with this code: #include <iostream> using namespace std; class A
I'm having some difficulty writing this code in php. I want to create a
I've been having problems with this code I had spent the last 3 hours

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.