简体   繁体   中英

Threads join() a thread more than once

I'm trying to make two threads work simultaneous . The first one should increment a value each second, and print that value to screen. The second one, should, each time a value is incremented, ask for some input value, and than print that value.

When I run my code, the incrementation goes well, but the stdin just ask me for an input once, and than it simple doesn't go on the repeater.join() nevermore (but it goes in Try).

My main thread:

public class mainThread extends Thread
  {
    //global variables
public int contador=0;
public int maxcont=10;

public void incContador()
{
    this.contador++;
}

public void run()
{   
    Thread repeater = (new Thread(new repeatThread()));
    repeater.start();
    for(int i=0; i<maxcont; i++)
    {
        incContador();
        try{
            //It's asked to increment 1 unit each second
            Thread.sleep(1000);
        }catch(InterruptedException e){System.out.println("Deu erro!\n");}

        System.out.println(contador);
        try{
            //use my other thread
            repeater.join();
        }catch(InterruptedException e){System.out.println("Deu erro 2!\n");}
    }
} 



public static void main(String[] args)
{
    (new mainThread()).start();
}

}

My repeater thread

   public class repeatThread implements Runnable
   {
public void run()
{
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    String s = "";
        try{
            s = buf.readLine();  
            System.out.println("Input is: "+s);

        }
        catch(IOException e){System.out.println("MyThreadRepeater - readLine() method error");}
    }




public static void main(String[] args) 
{
    (new Thread(new repeatThread())).start();
}
}

I believe you've misunderstand what Thread#join() does. this line

repeater.join();

blocks the current thread you're executing in, and waits for the thread represented by the repeater Thread to end.

Your repeater Thread is using a repeatThread object which simply asks for input once and ends.

Why would you expect it to prompt more input from the user?

I did not get your question completely. But your repeater code only executes the

BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    String s = "";
        try{
            s = buf.readLine();  
            System.out.println("Input is: "+s);

        }
        catch(IOException e){System.out.println("MyThreadRepeater - readLine() method error");}
    }

code only once. Run it inside a loop to run it continuously.

Your repeatThread runs only once. You MUST create new repeatThread object every iteration if you don't want to chang it's code. I mean like:

public void run()
{   
    for(int i=0; i<maxcont; i++)
    {
        Thread repeater = (new Thread(new repeatThread()));
        repeater.start();
        incContador();
        try{
            //It's asked to increment 1 unit each second
            Thread.sleep(1000);
        }catch(InterruptedException e){System.out.println("Deu erro!\n");}

        System.out.println(contador);
        try{
            //use my other thread
            repeater.join();
        }catch(InterruptedException e){System.out.println("Deu erro 2!\n");}
    }
} 

You'd better use Barriers (ex. CyclicBarrier) when you need several threads to wait each other at specific point. I should also mention that Thread.sleep(1000); may keep thread sleeping AT LEAST for a second, but not exactly.

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