Home > Enterprise >  Thread printing extra lines in Java
Thread printing extra lines in Java

Time:12-01

I am running a program with threads, sleeping them for a random amount of time and then waking them up. When it reaches the end, it prints that the last thread is now awake 6 different times. It does not do this on any of the other ones. Does anyone know why it is doing this?

sampleThread.java


import java.util.Random;

public class sampleThread extends Thread{
    sampleThread thread;
    Random rand = new Random();

    public void run() {

        thread = new sampleThread();
        int randSleep = rand.nextInt(1000);

        System.out.println(thread.getName()   " is sleeping for "   randSleep   " milliseconds");

        try {
            Thread.sleep(randSleep);
            System.out.println(thread.getName()   " is NOW AWAKE");

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

}

driver.java


public class driver {

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        List<Future<?>> futArray = new ArrayList<>();
        ExecutorService es = Executors.newFixedThreadPool(6);

        sampleThread temp = new sampleThread();
        for (int i=0; i<120; i  ) {
            Future<?> future = es.submit(temp);
            futArray.add(future);
        }

output:

Thread-117 is sleeping for 547 milliseconds
Thread-117 is NOW AWAKE
Thread-118 is sleeping for 442 milliseconds
Thread-118 is NOW AWAKE
Thread-119 is sleeping for 182 milliseconds
Thread-119 is NOW AWAKE
Thread-120 is sleeping for 487 milliseconds
Thread-120 is NOW AWAKE
Thread-120 is NOW AWAKE
Thread-120 is NOW AWAKE
Thread-120 is NOW AWAKE
Thread-120 is NOW AWAKE
Thread-120 is NOW AWAKE

I expected it to just run through and print when each was sleeping and done. Instead, it prints the last thread several times.

EDIT:

If anyone was wondering, I changed

public class sampleThread extends Thread{
sampleThread thread;
Random rand = new Random();

public void run() {

    thread = new sampleThread();

Instead of putting sampleThread thread; outside of run() and thread = new sampleThread(); inside of run(), I simply combined them into one statement of sampleThread thread = new sampleThread(); and put the whole line into run. This seems to have fixed the issue.

CodePudding user response:

You are duplicating effort. The fact that you had 120 threads should have been a hint. You created a thread pool but then ran code that created an extra thread on each submission.

Simple solution:

  1. Don't extend Thread, implement Runnable, that's all you need.
  2. To get the thread name use Thread.currentThread().getName()

Like so:

public static class sampleThread implements Runnable {
     Random rand = new Random();

    public void run() {

         int randSleep = rand.nextInt(1000);

        System.out.println(Thread.currentThread().getName()   " is sleeping for "   randSleep   " milliseconds");

        try {
            Thread.sleep(randSleep);
            System.out.println(Thread.currentThread().getName()   " is NOW AWAKE");

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

}

Now the output is

pool-1-thread-1 is sleeping for 526 milliseconds
pool-1-thread-6 is sleeping for 497 milliseconds
pool-1-thread-4 is sleeping for 565 milliseconds
pool-1-thread-5 is sleeping for 978 milliseconds
pool-1-thread-2 is sleeping for 917 milliseconds
pool-1-thread-3 is sleeping for 641 milliseconds
pool-1-thread-6 is NOW AWAKE
pool-1-thread-6 is sleeping for 847 milliseconds
pool-1-thread-1 is NOW AWAKE
pool-1-thread-1 is sleeping for 125 milliseconds
pool-1-thread-4 is NOW AWAKE
pool-1-thread-4 is sleeping for 884 milliseconds
pool-1-thread-3 is NOW AWAKE
pool-1-thread-3 is sleeping for 245 milliseconds
pool-1-thread-1 is NOW AWAKE
.
.
.
pool-1-thread-3 is NOW AWAKE
pool-1-thread-3 is sleeping for 863 milliseconds
pool-1-thread-5 is NOW AWAKE
pool-1-thread-6 is NOW AWAKE
pool-1-thread-4 is NOW AWAKE
pool-1-thread-1 is NOW AWAKE
pool-1-thread-2 is NOW AWAKE
pool-1-thread-3 is NOW AWAKE

Showing that you used only the 6 threads you ordered.

  • Related