Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2007, 11:09 PM
Member
 
Join Date: Nov 2007
Posts: 4
elecrobot is on a distinguished road
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();


}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 01:11 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
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; } } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-21-2007, 12:07 AM
Member
 
Join Date: Nov 2007
Posts: 4
elecrobot is on a distinguished road
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;
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-21-2007, 08:28 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
So that the Entrance class could use the Garage reference, viz, "garage" to call the enter method in the Garage class.
Code:
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);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-23-2007, 12:13 AM
Member
 
Join Date: Nov 2007
Posts: 4
elecrobot is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-23-2007, 12:38 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-25-2007, 03:43 PM
Member
 
Join Date: Nov 2007
Posts: 4
elecrobot is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared Scientific Toolbox in Java 1.00 Java Tip Java Announcements 0 04-10-2008 05:16 PM
how to access shared file/folder in LAN ksheetiz Networking 0 04-03-2008 12:59 PM
how to use session variable in my problem Arif Baig JavaServer Pages (JSP) and JSTL 1 03-27-2008 08:23 AM
Shared Scientific Toolbox in Java 0.99 JavaBean Java Announcements 0 03-16-2008 09:38 PM
I have a problem with variable "i" silvia New To Java 2 08-08-2007 12:05 AM


All times are GMT +3. The time now is 10:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org