CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010

    Java Threads Semaphore

    Hello!! I have a folowing problem.
    I have 2 Threadclasses: FirstThread and SecondThread.

    class FirstThread extends Thread
    private void work()
    //content doesnt matter
    public void run()

    Class SecondThread has the same code.

    Class FirstAndSecondThreads with main():

    public class FirstAndSecondThreads
    public static final int N1 = 5;
    public static final int N2 = 4;
    public static void main(String[] args)

    for(int i = 0; i < N1; i++)
    //create and start 5 instatces of FirstThread

    for(int i = 0; i < N2; i++)
    //create and start 4 instatces of SecondThread
    Now i want that all Threads of class SecondThread work only after Threads from the class FirstThread. I need to complete this with Semaphore..
    Any Ideas? Thanks!!!

  2. #2
    Join Date
    May 2006

    Re: Java Threads Semaphore

    If this is a one off situation you would normally use a CountDownLatch and if it was a repeating situation you would normally use a CyclicBarrier.

    If you have to use a Semaphore for example because it's a homework question then read the API docs for Semaphore and in particular look at the constructor that allows you specify how many permits there are.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.