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 9085815

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:20:15+00:00 2026-06-16T21:20:15+00:00

I have created a test package that tries to simulate a multi-threaded application. The

  • 0

I have created a test package that tries to simulate a multi-threaded application. The assumption is have N number of threads accessing a shared resource ,in this case a method that will do an increment to an integer and check if the integer is odd or even. What I am trying to achieve is when the method result is equal to 50 ,the thread that is monitoring that value should be able to stop all the threads and shutdown the process. I hope this gives everyone a clear picture. Now the problem I am experiencing is the results differ from time to time, they are inconsistent. Below is the code that I have written. I have used the most Thread safe measures possible, so I believe.

public class ThreadTestOne {

private static final java.util.concurrent.atomic.AtomicInteger number= new java.util.concurrent.atomic.AtomicInteger(0);
private static boolean stop=false; 
private static String orginaltime=gettime("hh:mm:ss");

static {
    try{

        java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(6);
        executor.execute(new MasterThread(executor));
        executor.execute(new Thread(new child1(),"Thread1"));
        executor.execute(new Thread(new child2(),"Thread2"));
        executor.execute(new Thread(new child3(),"Thread3"));
        executor.execute(new Thread(new child4(),"Thread4"));
        executor.execute(new Thread(new child5(),"Thread5"));

    }catch(Exception e){
        e.printStackTrace();
    }
}

private static final class child1 implements java.lang.Runnable{
    public void run(){
        while(stop!=true){
            try{
                getCount(this.getClass().getSimpleName());
                Thread.sleep(0000);
            }catch(java.lang.InterruptedException e){
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

private static final class child2 implements java.lang.Runnable{
    public void run(){
        while(stop!=true){
            try{
                getCount(this.getClass().getSimpleName());
                Thread.sleep(0000);
            }catch(java.lang.InterruptedException e){
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

private static final class child3 implements java.lang.Runnable{
    public void run(){
        while(stop!=true){
            try{
                getCount(this.getClass().getSimpleName());
                Thread.sleep(0000);
            }catch(java.lang.InterruptedException e){
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

private static final class child4 implements java.lang.Runnable{
    public void run(){
        while(stop!=true){
            try{
                getCount(this.getClass().getSimpleName());
                Thread.sleep(0000);
            }catch(java.lang.InterruptedException e){
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

private static final class child5 implements java.lang.Runnable{
    public void run(){
        while(stop!=true){
            try{
                getCount(this.getClass().getSimpleName());
                Thread.sleep(0000);
            }catch(java.lang.InterruptedException e){
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

private static final class MasterThread implements java.lang.Runnable{
    private java.util.concurrent.ExecutorService MasterExecutor=null;
    private MasterThread(java.util.concurrent.ExecutorService executor){
        this.MasterExecutor=executor;
    }
    public void run(){
         while(stop!=true){
             if(ThreadTestOne.getCurrNumber()==50){
                 stop=true;
                 MasterExecutor.shutdown();
                 System.out.println("Amount of time taken to execute is " +ThreadTestOne.elapsedTime(orginaltime)+ " seconds");
                 System.exit(0);
             }
         }
    }
}

private static synchronized void getCount(String ThreadName){

    System.out.println(ThreadName+" value is "+incrementgetNumber()+getKind());
}

private static final int incrementgetNumber(){

    return number.incrementAndGet();
}

private static final int getCurrNumber(){

    return number.get();
}

private static final String getKind(){

    return ((getCurrNumber()% 2) == 0) ? " and is an even number." : " and is an odd number.";
}

/*private static final void StartThreads(Thread ... threads){
    for (Thread thread : threads) {
        String ThreadName=thread.getName();
        long ThreadID=thread.getId();
        thread.start();
        System.out.println(ThreadName+" of ThreadID: "+ThreadID+" has been Started");
    }
}

private static final void StopThreads(Thread ... threads){
    for (Thread thread : threads) {
        if(thread!=null){
            String ThreadName=thread.getName();
            long ThreadID=thread.getId();
            System.out.println(ThreadName+" of ThreadID: "+ThreadID+" attempting to stop");
            thread.interrupt();
            System.out.println(ThreadName+" of ThreadID: "+ThreadID+" stopped");
        }
    }
}*/

private static long elapsedTime(String OriginalTime){
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("hh:mm:ss");
    long difference = 0;

    try {
         java.util.Date origTime = format.parse(OriginalTime);
         java.util.Date currTime = format.parse(gettime("hh:mm:ss"));
          difference = (currTime.getTime() - origTime.getTime())/1000; 
    } catch (Exception e) {
         e.printStackTrace();
    }
    return difference;
}

private static String gettime(String pattern){
    String timeString="";
    try{
     java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(pattern);
     java.util.Date currentTime = new java.util.Date();
     timeString = format.format(currentTime);
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return timeString;
}

public static void main (String []args){
    new ThreadTestOne();
}

}

The results I get are as follows

1st Test run

    child5 value is 1 and is an odd number.
child1 value is 2 and is an even number.
child1 value is 3 and is an odd number.
child1 value is 4 and is an even number.
child5 value is 5 and is an odd number.
child5 value is 6 and is an even number.
child5 value is 7 and is an odd number.
child5 value is 8 and is an even number.
child5 value is 9 and is an odd number.
child5 value is 10 and is an even number.
child5 value is 11 and is an odd number.
child5 value is 12 and is an even number.
child5 value is 13 and is an odd number.
child5 value is 14 and is an even number.
child5 value is 15 and is an odd number.
child5 value is 16 and is an even number.
child5 value is 17 and is an odd number.
child5 value is 18 and is an even number.
child5 value is 19 and is an odd number.
child5 value is 20 and is an even number.
child5 value is 21 and is an odd number.
child5 value is 22 and is an even number.
child5 value is 23 and is an odd number.
child5 value is 24 and is an even number.
child5 value is 25 and is an odd number.
child5 value is 26 and is an even number.
child5 value is 27 and is an odd number.
child5 value is 28 and is an even number.
child5 value is 29 and is an odd number.
child5 value is 30 and is an even number.
child5 value is 31 and is an odd number.
child5 value is 32 and is an even number.
child5 value is 33 and is an odd number.
child5 value is 34 and is an even number.
child5 value is 35 and is an odd number.
child5 value is 36 and is an even number.
child5 value is 37 and is an odd number.
child5 value is 38 and is an even number.
child5 value is 39 and is an odd number.
child5 value is 40 and is an even number.
child5 value is 41 and is an odd number.
child5 value is 42 and is an even number.
child5 value is 43 and is an odd number.
child5 value is 44 and is an even number.
child5 value is 45 and is an odd number.
child5 value is 46 and is an even number.
child5 value is 47 and is an odd number.
child5 value is 48 and is an even number.
child5 value is 49 and is an odd number.
child5 value is 50 and is an even number.
child4 value is 51 and is an odd number.
child2 value is 52 and is an even number.
child3 value is 53 and is an odd number.
child1 value is 54 and is an even number.
Amount of time taken to execute is 0 seconds

2nd Test Run

    child2 value is 112549 and is an odd number.
child2 value is 112550 and is an even number.
child2 value is 112551 and is an odd number.
child2 value is 112552 and is an even number.
child2 value is 112553 and is an odd number.
child2 value is 112554 and is an even number.
child2 value is 112555 and is an odd number.
child2 value is 112556 and is an even number.
child2 value is 112557 and is an odd number.
child2 value is 112558 and is an even number.
child2 value is 112559 and is an odd number.
child2 value is 112560 and is an even number.
child2 value is 112561 and is an odd number.
child2 value is 112562 and is an even number.
child2 value is 112563 and is an odd number.
child2 value is 112564 and is an even number.
child2 value is 112565 and is an odd number.
child2 value is 112566 and is an even number.
child2 value is 112567 and is an odd number.
child2 value is 112568 and is an even number.
child2 value is 112569 and is an odd number.
child2 value is 112570 and is an even number.
child2 value is 112571 and is an odd number.
child2 value is 112572 and is an even number.
child2 value is 112573 and is an odd number.
child2 value is 112574 and is an even number.
child2 value is 112575 and is an odd number.
child2 value is 112576 and is an even number.
child2 value is 112577 and is an odd number.
child2 value is 112578 and is an even number.
child2 value is 112579 and is an odd number.
child2 value is 112580 and is an even number.
child2 value is 112581 and is an odd number.
child2 value is 112582 and is an even number.
child2 value is 112583 and is an odd number.
child2 value is 112584 and is an even number.
child2 value is 112585 and is an odd number.
child2 value is 112586 and is an even number.
child2 value is 112587 and is an odd number.
child2 value is 112588 and is an even number.
child2 value is 112589 and is an odd number.
child2 value is 112590 and is an even number.
child2 value is 112591 and is an odd number.
child2 value is 112592 and is an even number.
child2 value is 112593 and is an odd number.
child2 value is 112594 and is an even number.
child2 value is 112595 and is an odd number.
child2 value is 112596 and is an even number.
child2 value is 112597 and is an odd number.
child2 value is 112598 and is an even number.
child2 value is 112599 and is an odd number.
child2 value is 112600 and is an even number.
child2 value is 112601 and is an odd number.
child2 value is 112602 and is an even number.
child2 value is 112603 and is an odd number.
child2 value is 112604 and is an even number.
child2 value is 112605 and is an odd number.
child2 value is 112606 and is an even number.
child2 value is 112607 and is an odd number.
child2 value is 112608 and is an even number.
child2 value is 112609 and is an odd number.
child2 value is 112610 and is an even number.
child2 value is 112611 and is an odd number.
child2 value is 112612 and is an even number.
child2 value is 112613 and is an odd number.
child2 value is 112614 and is an even number.
child2 value is 112615 and is an odd number.
child2 value is 112616 and is an even number.
child2 value is 112617 and is an odd number.
child2 value is 112618 and is an even number.
child2 value is 112619 and is an odd number.
child2 value is 112620 and is an even number.
child2 value is 112621 and is an odd number.
child2 value is 112622 and is an even number.
child2 value is 112623 and is an odd number.
child2 value is 112624 and is an even number.
child2 value is 112625 and is an odd number.
child2 value is 112626 and is an even number.
child2 value is 112627 and is an odd number.
child2 value is 112628 and is an even number.
child2 value is 112629 and is an odd number.
child2 value is 112630 and is an even number.
child2 value is 112631 and is an odd number.
child2 value is 112632 and is an even number.
child2 value is 112633 and is an odd number.
child2 value is 112634 and is an even number.
child2 value is 112635 and is an odd number.
child2 value is 112636 and is an even number.
child2 value is 112637 and is an odd number.
child2 value is 112638 and is an even number.
child2 value is 112639 and is an odd number.
child2 value is 112640 and is an even number.
child2 value is 112641 and is an odd number.
child2 value is 112642 and is an even number.
child2 value is 112643 and is an odd number.
child2 value is 112644 and is an even number.

3rd Test Run

    child4 value is 1 and is an odd number.
child1 value is 2 and is an even number.
child3 value is 3 and is an odd number.
child3 value is 4 and is an even number.
child3 value is 5 and is an odd number.
child3 value is 6 and is an even number.
child3 value is 7 and is an odd number.
child3 value is 8 and is an even number.
child3 value is 9 and is an odd number.
child3 value is 10 and is an even number.
child3 value is 11 and is an odd number.
child3 value is 12 and is an even number.
child3 value is 13 and is an odd number.
child3 value is 14 and is an even number.
child3 value is 15 and is an odd number.
child3 value is 16 and is an even number.
child3 value is 17 and is an odd number.
child3 value is 18 and is an even number.
child3 value is 19 and is an odd number.
child3 value is 20 and is an even number.
child3 value is 21 and is an odd number.
child3 value is 22 and is an even number.
child3 value is 23 and is an odd number.
child3 value is 24 and is an even number.
child3 value is 25 and is an odd number.
child3 value is 26 and is an even number.
child3 value is 27 and is an odd number.
child3 value is 28 and is an even number.
child3 value is 29 and is an odd number.
child3 value is 30 and is an even number.
child3 value is 31 and is an odd number.
child3 value is 32 and is an even number.
child3 value is 33 and is an odd number.
child3 value is 34 and is an even number.
child3 value is 35 and is an odd number.
child3 value is 36 and is an even number.
child3 value is 37 and is an odd number.
child3 value is 38 and is an even number.
child3 value is 39 and is an odd number.
child3 value is 40 and is an even number.
child3 value is 41 and is an odd number.
child3 value is 42 and is an even number.
child3 value is 43 and is an odd number.
child3 value is 44 and is an even number.
child3 value is 45 and is an odd number.
child3 value is 46 and is an even number.
child3 value is 47 and is an odd number.
child3 value is 48 and is an even number.
child3 value is 49 and is an odd number.
child3 value is 50 and is an even number.
Amount of time taken to execute is 0 seconds
child2 value is 51 and is an odd number.

If you look at the 1st and 3rd test runs you will see that the threads continue to execute even after the 50 mark has been reached.
The 2nd test run was baffling. Kindly suggest what the problem could be ?

  • 0 0 Answers
  • 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-16T21:20:16+00:00Added an answer on June 16, 2026 at 9:20 pm

    stop must be volatile in order for modifications to it to be guaranteed to be visible across threads.

    note, since the master thread check and stop operations are not atomic, there’s still not going to be exactly 50 results in each run. also, it’s entirely possible that the master thread will never see the value exactly equal to 50, in which case it will run forever.

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

Sidebar

Related Questions

I have my new macbook air. I have created one test application. now i
I have created a test table in MySQL and would like to insert 10
I have created 5 test cases in JUnit 4 and I was wondering is
I have created total 50 test scripts. All these scripts use almost same objects
I have created a little test project to try to resolve a problem I
I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
I have created a model test case in a plugin. Plugin name: Filters and
I have created a unit test in my c# project which builds into a
I am using SQL Server 2008 R2, and I have created a schema Test
I have created a fiddle to test this : http://jsfiddle.net/Ninjanoel/X6ShS/ which works fine :(

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.