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:
Code Java:
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.
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.
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
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.
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.
Code Java:
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();
}
}
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.