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

  • SEARCH
  • Home
  • 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 5937947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:36:52+00:00 2026-05-22T15:36:52+00:00

I have an application in java that uses two different threads.And for sharing data

  • 0

I have an application in java that uses two different threads.And for sharing data between these two threads I wanna use a Singleton class.

The data that needs to be shared between threads is (Latitude,Longitude).

And here is my problem:What should I use for storing this data in the Singleton class?

Some Lists..?

And if you can also provide an example it would be fantastic.Thank you!

Is this Singleton ????How could I make it non-Singleton

EDIT:

I have a java app that does the following thing:

1.The first thread(which is a ThreadPool)-which I believe will also write in the BlockingQ
Listens to one port for incoming connections from five different users
Let’s call them :

user1
user2
user3
user4
user5

each of them sending GPS data.

2.The second thread-which will read from the BlockingQ.
In the same time my java app listens to a second port where waits for another client(different from those who send GPS data) to connect to it.

Now…I have a second app that connects to the java app that I’ve just described it.

In this second app I have a list user1…user5 and depinding on which item I will choose(user1…5) I have to receive the correct data from there.

So now….how do I write/read the data in the BlockingQ in order for me to receive the correct data???

  • 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-05-22T15:36:53+00:00Added an answer on May 22, 2026 at 3:36 pm

    I think what you are trying to achieve is a Producer Consumer Pattern. You should use a BlockingQueue and an immutable Coordinate object with Latitude and Longitude for that.

    Coordinate should only have the two fields Longitude and Latitude that are both final fields and can only be set with the constructor. This way you can make sure, that you don’t have any race conditions or change the fields by accident with the wrong thread.

    BlockingQueue does not store the data permanently, so only if you need to do that you could create a Singleton.
    The better solution would be to create a class like ShareData and just pass them to the two threads so you can save yourself some time creating a threadsafe Singleton. This could look like this:

    public interface ShareData {
        public void AddToList(Coordinate coord); // add to BlockingQueue and an internal store here
        public Coordinate TakeFromList();  // take from BlockingQueue
    }
    

    The Coordinate class should look like this and is threadsafe, since it is immutable and cannot change its value.

    public class Coordinate {
      private final double lon;
      private final double lat;
    
      public Coordinate(double lon, double lat) {
        this.lon = lon;
        this.lat = lat;
      }
    
      public double getLon() {
        return lon;
      }
    
      public double getLat() {
        return lat;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a java web service application that uses log4j to do logging. An
I have a Java application that launches another java application. The launcher has a
I have a Java application that monitors a folder for incoming XML files. When
I have a Java application that's very String-heavy - it takes a feed of
I have a java application that has Web Services published using Axis. With the
I have a java application that is connected to a view on a remote
We have a Java application that needs to be brought to the foreground when
At a new job I started, we have both a Java application that handles
I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons,
I have an application that's a mix of Java and C++ on Solaris. The

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.