简体   繁体   中英

My timer's thread may be cheating on him with Thread.sleep?

So essentially I am concerned that my timertask's run method is not being called after 10 minutes because I am putting the main thread to sleep for 10 seconds to avoid crazy CPU usage to just run through an empty while loop all day. Here is the code for the java main method.

 private static boolean testDone = false;
 public static void main(String[] args)
 {
        final int minutes = 10;
        final StressTest test = new StressTest(someParams);

        test.start();

        Timer timer = new Timer();
        timer.schedule(new TimerTask(){

            @Override
            public void run() {

                testDone = true;
                int completedSynths = test.stop();
                System.out.println("Completed Synths: " + completedSynths);
                System.out.println("Elapsed Time in Minutes: " + minutes);
                System.out.println("Throughput (synths/min): " + completedSynths/minutes);

            }}, minutes*60*1000);

        while(!testDone)
        {
            System.out.println("Still not done... sleeping for 10 secs....");
            Thread.sleep(10000);
        }

        System.exit(0);

Even crazier, the System.out in the while loop is never printing. What have I done??

EDIT: TO add pseudocode for StressTest object

public class StressTest 
{

    private SecureRandom random = new SecureRandom();

    private volatile int completedSynths = 0;

    private volatile boolean shouldStop = false;

    private Thread[] threads;

    /**
     * Instantiate a new StressTest object.
     * 
     * @param threadCount number of concurrent threads to be polling server
     */
    public StressTest(int threadCount)
    {
        threads = new Thread[threadCount];
    }

    public void start()
    {
        System.out.println("Starting Stress Test....");

        for(int i = 0; i < threads.length; i++)
        {
            Runnable synthCaller = new SynthApiCaller();
            threads[i] = new Thread(null, synthCaller, "SynthThread" + i);
            threads[i].run();
        }
    }

    public int stop()
    {
        System.out.println("Stopping Stress Test...");
        shouldStop = true;

        return completedSynths;
    }

    private String randId()
    {
        return new BigInteger(130, random).toString(32);
    }

    private class SynthApiCaller implements Runnable
    {
        @Override
        public void run() 
        {
            while(!shouldStop)
            {
                try 
                {
                            //this class makes an HTTP request to a server and then writes result to a file
                    MyClass.writeFile( "someFileLoc/file.data");
                    completedSynths++;

                    Runtime.getRuntime().exec("rm -r " + fileLoc);
                    System.out.println("Synth # " + completedSynths);
                } 
                catch (IOException e) 
                {
                    System.out.println(e.getMessage());
                }
            }

            System.out.println("Thread terminated...");
        }

    }
}

I am concerned that my timertask's run method is not being called after 10 minutes because I am putting the main thread to sleep for 10 seconds

The Thread.sleep(...) in your main thread will not affect the running of the Timer . If the timer is not running after 10 minutes then is it possible that test.stop() is blocking?

It is important to realize that if the test is started in the main-thread and then is being stopped in the Timer thread then some synchronization will be necessary. I assume the test is running in another thread. You probably will need it to be synchronized then inside of the Timer thread you would call something like:

synchronized (test) {
    test.start();
}

If you are new to Java synchronization, there are some good tutorials online .

If you are wondering whether or not the timer is being called at all, I'd set a break point inside your timer task where is sets testDone = true and see if it gets there. Here's a good tutorial of using a debugger in eclipse .

Even crazier, the System.out in the while loop is never printing. What have I done??

As @assylias mentioned, the System.out(...) in your while loop not showing up must mean that testDone is set to true. Since testDone is being updated and accessed in different threads, you need to make sure it is also volatile .


I just ran your code sample without the test.start() and stop() and it seems to work fine. The problem may be in your test code.

Still not done... sleeping for 10 secs....
Still not done... sleeping for 10 secs....
...
Completed Synths: 1
Elapsed Time in Minutes: 10
Throughput (synths/min): 0

Now that you've added more code, here are some comments:

  • completedSynths++; should be changed to be an AtomicInteger . ++ is not an atomic operation so even tho the field is volatile, multiple threads can overwrite each other's increment.

  • If you are trying to wait for the threads to complete, instead of sleeping for 10 minutes, I'd recommend calling thread[i].join() with the threads. Even better would be use an ExecutorService and use the awaitTermination(...) method.

  • You call shouldStop = true; and then return the completedSynths; . You may want to wait for the threads to finish or something.

  • I'd not pass in a null ThreadGroup to the Thread constructor. Just use the constructor without the ThreadGroup .

I suggest making testDone volatile. As it is, I don't see anything forcing changes to testDone to be visible to reads in threads other than the one making the change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM