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 7659375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:19:29+00:00 2026-05-31T13:19:29+00:00

I am new to Java Multithreading World, I wrote this program, I just wanted

  • 0

I am new to Java Multithreading World, I wrote this program, I just wanted to make sure it is multithreaded program or not. In this I am creating 30 tasks that are executed by 10 threads. So this implementation is right or not? I think you can get to know what I am doing in my code. I am just generating some random ip address and passing it to method and see what’s the average time for each call after returning from the method. SO for that I wrote all the task in my run method. Any suggestions will be appreciated.

public class Testing {

public static void main(String[] args) throws InterruptedException {
    int size = 10;

    // create thread pool with given size
    ExecutorService service = Executors.newFixedThreadPool(size); 

    // queue some tasks
    for(int i = 0; i < 3 * size; i++) {
        service.submit(new ThreadTask(i));
    }


    // wait for termination        
    service.shutdown();
    service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }
}


class ThreadTask implements Runnable {
    private int id;

    public ThreadTask(int id) {
        this.id = id;
    }

    public void run() {
        System.out.println("I am task " + id);

        Map<Long, Long> histgram = new HashMap<Long, Long>();
        Set<String> ipNull = new HashSet<String>();

        GetLocationByIpResponse resp = null;

        long total = 10000;
        long difference = 0;
        long found = 0;
        long found_country = 0;
        long runs = total;
        try {
            long start_total = System.nanoTime();
            while(runs > 0) {

                String ipAddress = generateIPAddress();

                long start_time = System.nanoTime();

                resp = PersonalizationGeoLocationServiceClientHelper.getLocationByIp(ipAddress);

                long end_time = System.nanoTime();

                if(resp.getLocation() != null) {
                 difference = (end_time - start_time)/1000000;
                } else if(resp.getLocation() == null) {
                  difference  = 0;
                } 

                printResult(ipAddress, resp, difference);


                Long count = histgram.get(difference);
                if (count != null) {
                    count++;
                    histgram.put(Long.valueOf(difference), count);
                } else {
                    histgram.put(Long.valueOf(difference), Long.valueOf(1L));
                }

                runs--;

            }
            long end_total = System.nanoTime();

            long finalTotal = (end_total - start_total)/1000000;

            float avg = (float)(finalTotal) / total;

            Set<Long> keys = histgram.keySet();

            for (Long key : keys) {
                Long value = histgram.get(key);
                System.out.println("$$$GEO OPTIMIZE SVC MEASUREMENT$$$, HG data, " + key + ":" + value);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ServiceException e) {
            e.printStackTrace();
        }


    }
  • 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-31T13:19:30+00:00Added an answer on May 31, 2026 at 1:19 pm

    There doesn’t seem to be anything wrong with the multi-threading as such, but there are three potential issues.

    1. You’re measuring time elapsed. This can be unreliable at the best of times (as there are many other threads running on your computer) but if you have multiple Java threads running in parallel, it can make your measurements even less accurate.
      So for this kind of task maybe a single-threaded approach works better, but don’t take my word for it: replace the random generation of IPs with a fixed list that you go through and try running the same measurements with varying numbers of threads.

    2. I have no way to judge whether the methods that you call from run() are themselves thread safe or not. When you write multi-threaded code, check carefully that everything you call is thread safe as well (for example SimpleDateFormat is not).

    3. Following on from the previous point: System.out.println() isn’t thread safe, so you might end up with one message cutting in the middle of another. It’s not the end of the world but you probably don’t want a cluttered output.

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

Sidebar

Related Questions

I am new to Multithreading world, I am not sure how should I write
I'm new to Java, and I need some help working on this program. This
I'm kinda new to Java, so haven't yet fully grasped the concept of multithreading.I
When creating a new Java project in IntelliJ IDEA, the following directories and files
I'm new to multithreading in Perl and looking for something similar to Java's thread
I'm not looking for a Neural Networks library, since I'm creating new kinds of
I have to write this produce consumer application using multithreading. I wrote the following
Actually I wrote a Java program for calculating a particular number on the Fibonacci
I am relativity new to multithreading in Java, and I would like to know
I'm creating a new Java project for a concept/construct that's usually referred to with

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.