Java Threads Semaphore
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Java Threads Semaphore

  1. #1
    Join Date
    Dec 2010
    Posts
    2

    Java Threads Semaphore

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

    class FirstThread extends Thread
    {
    //constructor
    private void work()
    {
    //content doesnt matter
    }
    public void run()
    {
    work();
    }
    }

    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
    Location
    UK
    Posts
    4,473

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center