|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-19-2007, 11:09 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 4
|
|
|
Shared variable mutual exclusion problem. Plz help
Hi Everyone!
I am very new to Java programming, therefore i apologize for any stupid question and i will really appreciate comments from anyone.
My problem is I am trying to make a parking facility on java which will have two enterances and two exits. when i run the thread the total ammount of cars in the parking space is not accurate. I guess it is the mutual exclusion problem which i am facing. Now i am trying to solve it using synchronisation. Could anyone please let me know what further steps do i need to take to solve this problem.
My program structure is i have a class for one shared variable i.e. Total. which is being used by both entrance and exit classes. i think this shared variable i.e. Total needs to by synchronised. My code for entrance and shared variable class is as follows:-
Shared variable class:-
public class variable
{
public static int Total;
}
Entrance class:-
public class Entrance extends Thread
{
int value1;
int X1;
variable S;
public Entrance()
{
variable S= new variable();
}
public Entrance(int ThreadName,int max)
{
X1=ThreadName;
value1=max;
}
public void run()
{
for(int Y1=0;Y1<value1;Y1++)
{
if(S.Total<50)
{
S.Total++;
System.out.println("Car in at "+X1+", Total =" +S.Total+"{"+(Y1+1)+"}");
}
}
}
}
object class:-
public class objects
{
variable S;
public objects()
{
S.Total=0;
Entrance Entrance1=new Entrance(1,50);
Entrance1.start();
Exit Exit1=new Exit(1,50);
Exit1.start();
Entrance Entrance2=new Entrance(2,50);
Entrance2.start();
Exit Exit2=new Exit(2,50);
Exit2.start();
}
}
|
|

11-20-2007, 01:11 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 910
|
|
import java.util.Random;
public class GarageTest
{
public static void main(String[] args)
{
Garage garage = new Garage();
Entrance entrance1 = new Entrance(garage, 1);
entrance1.start();
Exit exit1 = new Exit(garage, 1);
exit1.start();
Entrance entrance2 = new Entrance(garage, 2);
entrance2.start();
Exit exit2 = new Exit(garage, 2);
exit2.start();
}
}
class Garage
{
public static int TOTAL = 10;
int currentNumber = 0;
public synchronized void enter(int id, int carNumber) {
while (currentNumber == TOTAL) {
try {
wait();
} catch (InterruptedException e) { }
}
currentNumber++;
System.out.printf("Car %2d entered at %d " +
"currentNumber = %d%n",
carNumber, id, currentNumber);
notifyAll();
}
public synchronized void exit(int id) {
while (currentNumber == 0) {
try {
wait();
} catch (InterruptedException e) { }
}
currentNumber--;
System.out.println("Car exited at " + id +
" currentNumber = " + currentNumber);
notifyAll();
}
}
class Entrance extends Thread
{
Garage garage;
int number;
public Entrance(Garage garage, int threadNum)
{
this.garage = garage;
number = threadNum;
}
public void run()
{
for(int j = 0; j < 10; j++)
{
garage.enter(number, j+1);
}
}
}
class Exit extends Thread
{
Garage garage;
int number;
Random seed = new Random();
public Exit(Garage garage, int threadNum)
{
this.garage = garage;
number = threadNum;
}
public void run() {
for (int j = 0; j < 10; j++) {
garage.exit(number);
try {
sleep(2000 + seed.nextInt(3000));
} catch (InterruptedException e) {
break;
}
}
}
}
|
|

11-21-2007, 12:07 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 4
|
|
|
Thanks hardwired!!!... i found the problem in my program. Could you please let me know why is it necessary to add garage in the constructor below:- Thanks for your help
public Entrance(Garage garage, int threadNum)
{
this.garage = garage;
number = threadNum;
}
|
|

11-21-2007, 08:28 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 910
|
|
So that the Entrance class could use the Garage reference, viz, "garage" to call the enter method in the Garage class.
class Entrance extends Thread
{
Garage garage;
public Exit(Garage garage, int threadNum)
{
this.garage = garage;
...
public void run()
{
for(int j = 0; j < 10; j++)
{
garage.enter(number, j+1);
|
|

11-23-2007, 12:13 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 4
|
|
|
gr8 stuff i got it. Now if i make my enter and exit methods static and delete the Garage garage from the constructor, do u suggest it to be a good alternative? well i tried it. it gives me an error that non-static method i.e.notifyAll and wait cannot be used in static.
its just i want to learn different ways of doing. Could you plz shed some light on this. lets say i definately want to use this alternative method, then how can i do this.
Kind Regards,
elecrobot
|
|

11-23-2007, 12:38 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 910
|
|
Now if i make my enter and exit methods static and delete the Garage garage from the constructor, do u suggest it to be a good alternative?
No.
well i tried it. it gives me an error that non-static method i.e.notifyAll and wait cannot be used in static.
That makes sense. You can look up these methods in the Object class api to see that they are both declared public.
There may be a number of other ways you can do this. For more about this try Lesson: Concurrency.
|
|

11-25-2007, 03:43 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 4
|
|
|
absolute great stuff. now i have further played with the program and have found few more question. that would be really kind of you if you plz explain me the following questions:-
1)if i write garage=garage instead of this.garage=garage, the code still works but total i.e. current number goes into negative. is there any logic behind it or i m making another mistake in my program?
2)why do we need following code:-
public static void main(String[] args)..if i replace this with
public GarageTest()...code still works perfectly fine.
3)sometimes in method we write void and sometimes don't, what does void really means?
4)why do we really need currentNumber, why can't we directly use if(Total==50) instead of if(currentNumber==50).
Thanks a lot for your help. really appreciate.
Kind Regards,
elecrobot
Last edited by elecrobot : 11-26-2007 at 11:59 PM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|