Jump to content
    
    public static void main(String[] args) {

        for(int i = 0; i < NUM_THREADS_T2; i++){
            Thread t1 = new Thread(new ThreadObject(m, channel, i));
            t1.start();
        }


        for(int i = 0; i < NUM_THREADS_T2; i++){
            Thread t2 = new Thread(new ThreadObjectT2(m));
            t2.start();

        }
        
        Thread leader = new Thread(new Centra(m));
        leader.start();
    }

}

I am trying to use both CylicBarrier and CountDownLatch but I am unable to get how to do it when starting them in loops like this. I want to keep the loops as it simplifies the process for me. What I'm trying to do is tell t2 to wait for t1 to finish before it starts. This also goes for the leader to wait for a t2 thread to finish before it starts. Anyone got any tips or hints on how to solve it in a good manner? I've been on Google and found some examples but I have not been able to solve it so far.

 

Thanks in advance

OLS

| Intel i5 4670k @ 4.3GHz | XFX RX 480 8 GB | Asus Z87i-Pro | 8.0 GB Kinston DDR3 | Samsung Evo 120GB SSD |

Link to comment
https://linustechtips.com/topic/775617-java-threads-help/
Share on other sites

Link to post
Share on other sites

if you're waiting for a thread to finish before doing the next thread are you sure you need to even use them? you're not running anything concurrently. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/775617-java-threads-help/#findComment-9786113
Share on other sites

Link to post
Share on other sites

3 minutes ago, vorticalbox said:

if you're waiting for a thread to finish before doing the next thread are you sure you need to even use them? you're not running anything concurrently. 

He starts many threads per loop, and if I understood him correctly, he want to wait for them after each loop.

Link to comment
https://linustechtips.com/topic/775617-java-threads-help/#findComment-9786131
Share on other sites

Link to post
Share on other sites

6 minutes ago, Mr_KoKa said:

He starts many threads per loop, and if I understood him correctly, he want to wait for them after each loop.

it seemed that he was creating a controller which started thread 1 which after starts thread 2. Doesn't seem worth threading it, if this is correct. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/775617-java-threads-help/#findComment-9786167
Share on other sites

Link to post
Share on other sites

50 minutes ago, Mr_KoKa said:

He starts many threads per loop, and if I understood him correctly, he want to wait for them after each loop.

Correctly, T1 has a certain process it has to go through and that takes abit of time. And if I don't tell T2 to wait for T1, T2 would be finished before T1 is finished, meaning that it wouldnt do what I want it to do. But I got CountDownLatch working so I'm good! :)

| Intel i5 4670k @ 4.3GHz | XFX RX 480 8 GB | Asus Z87i-Pro | 8.0 GB Kinston DDR3 | Samsung Evo 120GB SSD |

Link to comment
https://linustechtips.com/topic/775617-java-threads-help/#findComment-9786327
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×