Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with Synchronisation with a semaphore

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Help with Synchronisation with a semaphore

    Hey,

    Recently been introduced to threads & came across a question involving the usage of semaphores. I'm just a bit confused as to how to use them and was wondering if anybody could shed a bit of guidance for me. I'll post the question to begin with:

    Create a program using 2 seperate threads, each printing out a message when it reaches a particular part in its run method. Add a Java semaphore to the program and use it to ensure that whenever you run the program the point A is always reached before point B i.e. every time you run the program it behaves as follows:

    $ java Points
    Point A reached
    Point B reached
    Create and initialise your semaphore in the main method and pass it to each thread.

    I'm unsure how & where to use this semaphore and also what parameter i'm supposed to give it.

    My code so far is:

    import java.util.concurrent.Semaphore;
     
    class PointA extends Thread {
     
    	public PointA() {
    	}
     
    	public void run() {
     
    		try {
    			sleep((int) (Math.random() * 100));
    		} catch (InterruptedException e) {
    		}
     
    		System.out.println("Point A reached");
     
    	}
    }
     
    class PointB extends Thread {
     
    	public PointB() {
     
    	}
     
    	public void run() {
     
    		try {
    			sleep((int) (Math.random() * 100));
    		} catch (InterruptedException e) {
    		}
     
    		System.out.println("Point B reached");
     
    	}
    }
     
    public class Points {
     
    	public static void main(String[] args) throws InterruptedException {
     
    		PointA a = new PointA();
    		PointB b = new PointB();
     
    		Semaphore sem = new Semaphore(0);
     
    		b.start();
    		a.start();
     
    		b.join();
    		a.join();
    	}
    }

    Thanks in advance for any help.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Help with Synchronisation with a semaphore

    You can use the Semaphore in a wait/notify context...for instance, Thread B calls wait...once thread A completes it calls notify on the semaphore.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Synchronisation with a semaphore

    How would I do this? I don't expect you to spoon feed but I'm really lost sorry. We've been taught little to nothing about threads and semaphores and I can't seem to find any useful information to explain even the basics.
    For example:

    Semaphore sem = new Semaphore(1);

    What number are you supposed to pass through as a parameter? And where am I supposed to call these wait(), notify() methods because every time I try, I'm getting IllegalMonitorStateExceptions

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Help with Synchronisation with a semaphore

    Sorry, didn't initially see you were using the Semaphore class. This class facilitates acquiring locks to a certain number of threads. In your case you could create a Semaphore of size 1, and pass this to the Threads. You can then let A acquire - this acquires the only space so when B attempts to acquire the method will block until that single permit is released. When A is finished it can call release...the acquire method in B returns and B can do its thing.

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Synchronisation with a semaphore

    Thanks for the help. I'll post how I solved it below. By "solve" I mean it gets the required output in the sense that Point A is always reached before Point B, but would you be able to let me know if I did this correctly. Also, anytime i tried passing 1 as a parameter for the Semaphore it wouldn't work.. will only work if I pass 0. Why is this?
    Cheers.

    import java.util.concurrent.Semaphore;
     
    class PointA extends Thread {
    	private Semaphore sem;
     
    	public PointA(Semaphore sem) {
    		this.sem = sem;
    	}
     
    	public void run() {
     
    		try {
    			sleep((int) (Math.random() * 100));
    		} catch (InterruptedException e) {
    		}
    		sem.release();
    		System.out.println("Point A reached");
     
    	}
    }
     
    class PointB extends Thread {
    	private Semaphore sem;
     
    	public PointB(Semaphore sem) {
    		this.sem = sem;
    	}
     
    	public void run() {
    		try {
    			sem.acquire();
    		} catch(InterruptedException e) {
    			e.printStackTrace();
    		}
    		try {
    			sleep((int) (Math.random() * 100));
    		} catch (InterruptedException e) {
    		}
    		sem.release();
    		System.out.println("Point B reached");
     
    	}
    }
     
    public class Points {
     
    	public static void main(String[] args) throws InterruptedException {
    		Semaphore sem = new Semaphore(0);
    		PointA a = new PointA(sem);
    		PointB b = new PointB(sem);
     
     
    		b.start();
    		a.start();
     
    		a.join();
    		b.join();
    	}
    }

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Help with Synchronisation with a semaphore

    PointA is not acquiring the Semaphore - if you set the permit value to 1, and have the PointA class acquire the Semaphore prior to PointB, then PointB can only acquire the Semaphore after PointA releases the Semaphore.

Similar Threads

  1. Replies: 6
    Last Post: March 18th, 2010, 08:56 PM

Tags for this Thread