my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Monitor in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Monitor in Java" Watch "Monitor in Java" New topic
Author

Monitor in Java

Michael Beem
Greenhorn

Joined: Jan 15, 2003
Posts: 1
HI!
PLEASE,could anyone solve the problem of wolves and sheeps with the Monitor in Java
(the animals are transported by a boat which has 3 places ;it is not allowed the 2 wolfes and 1 sheep in boat,other combinations are allowed,when the boat is full it transports animals to the other shore and returns for new animals)
Thanks!!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
(a) Not without a more complete problem specification.
(b) Not if it's homework (which is what it sounds like). Hints or help may be appropriate, but this is really the sort fo thing where you should do most of the work yourself.


"I'm not back." - Bill Harding, Twister
Michael Been
Greenhorn

Joined: Jan 15, 2003
Posts: 5
b)you have wrigte,it is my homework,but i work on it.
a)i do not understand do I need 2 threads(wolfes and sheeps) or 3(one for the boat)?(because using semaphore in c i have 3 proceses)I made example with two threads but it's work strange.
I znderstand ProducerConsumer prog,but i just can't understood this.So please,if you want and can,give me some help.
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Hi Micheal,
Here�s some hint:
Implement the boat as a buffer class (monitor), three sockets therein. class Sheep extends Thread; class Wolf extends Thread. There could be one counter for each of wolf instance and sheep instance in the monitor. synchronize both the sheep.board() and wolf.board() in the monitor.

Good Luck,
Ellen
ps: Do you have to implement it with monitor? I guess maybe you can try barrier, not sure. Why semaphore there?
[ January 15, 2003: Message edited by: Ellen Fu ]
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Hi again,
I started to work on it, but soon felt it has to be treated as a barrier model. In a monitor, the threads pass one by one, don�t have to wait for the full-sockets condition( In your question, it is "the boat transports the animals when it is full"). Don�t know how to meet the specification with monitor, sorry.

Enough work today...rather tired now, gotta sleep....
Good Luck,
Ellen
[ January 15, 2003: Message edited by: Ellen Fu ]
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
a new idea: always first allot two places in the boat to two sheep, then let the third be no matter sheep or wolf. The order should be forced. To implement this idea: create a barrier for sheep threads, this barrier waits for 2 threads; create another one-socket monitor, both sheep and wolf threads can pass this monitor. But now I don't know how to force the order between barrier and monitor, AND make exactly three threads rendezvous before the boat is reset(leave for the other shore).
Ach, if not threads, it's actually a very simple exercise. 0 for sheep and 1 for wolf, a random integer generator repeatedly generates 0 or 1 to choose sheep or wolf, a counter, then some simple logic control statements, done.
Your assignment is quite challenging to me. Could you please paste the solution here if you get it? Thank you.
Regards,
Ellen
[ January 16, 2003: Message edited by: Ellen Fu ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Could you please paste the solution here if you get it?
Perhaps it would help if we saw the problem first. How many wolves, how many sheep? What is the objective? Are there any other important rules which have not been stated?
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
How many wolves, how many sheep?
I guess it doesn�t matter. Just assume there are endlessly many animals( a server can always deal with requests from clients, regardless how many). The instructor just wanted us to transport them. According to the specification, the allowed combinations of animals in the boat are:
1. sheep sheep sheep
2. wolf sheep sheep
3. wolf wolf wolf
if and only if the boat is full ( there are 3 aminals ), can it leave for the other shore.
I read quite a few literatures on Java threads in library this morning in order to work out this problem, then got a question: Micheal, doesn�t your instructor think this problem is a bit too fancy for someone just started learning threads? I do found some models seemingly suitable for this problem, but they are for writing/optimizing compilers or to implement some rather complicated algorithms. Or, maybe I have some severe misconception so far.
Well, my keen eye on Gurus here....

Regards,
Ellen
[ January 16, 2003: Message edited by: Ellen Fu ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The problem is still very vague. Let's assume 3 wolves and three sheep. What's wrong with this solution?

I suspect this is not what's intended, but there are numerous variants of this sort of problem out there. If we don't know what the problem really is, there's little point in trying to discuss it.
[ January 16, 2003: Message edited by: Jim Yingst ]
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Hi Mr. Yingst,
sorry I misunderstood you. Yes you are right, the question is obscure.
In my assumption, this question is intended to simulate a one-way-handshake communication. The sender is the shore where countless wolves and sheep there. The receiver is the other shore. The question is equivalent to: There are countless sheep and wolves and also countless boats. Every boat has three places. The boats leaves the shore one by one. When a boat is full of animals( if and only if sss, wss, www), it leaves. When it reaches the other shore, it never returns. So that the animals are transported from one shore to the other.
In your assumption above, it looks like a two-way-handshake communication. I think a semaphore or a monitor will do, equipped with some logic control statements, might be a bit complex, not sure.
Thank you very much.
Regards,
Ellen
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ah, that makes more sense. The puzzle version which I remembered has additional rules like: 3 wolves, 3 sheep; you can't let wolves outnumber sheep on either shore or the boat (a rule I intentionally violated in my example above), and the boat won't go anywhere without at least one animal in it. This version isn't very suitable to solution by threads I think, so your interpretation is more likely.
Michael Been
Greenhorn

Joined: Jan 15, 2003
Posts: 5
Thank You for traing to help me.I solve my problem.
Here is the code:
class km1Monitor extends Object {
private int take_place = 0;
private int nmb_wolf=0;
private int br_misionara=0;
synchronized public void insertwolf() {
while (take_place==3 || (br_misionara==1& nmb_wolf==1)){
try{
wait();
} catch (InterruptedException ex) {};
}
take_place++;
nmb_wolf++;
System.out.println("Enters wolf.");
if(take_place==3){take_place=0; nmb_wolf=0;nmb_sheap=0;System.out.println("Transport anb back with empty boat i");notify();}
}
synchronized public void insertsheap() {
while (take_place==3 &nmb_wolf==2) {
try{
wait();
} catch (InterruptedException ex) {};
}
take_place++;
nmb_sheap++;
System.out.println("Enters sheap.");
if(take_place==3){take_place=0; nmb_wolf=0;nmb_sheap=0;System.out.println("Transport and back with empty boat");notify();}
}
}

public class km1 extends Thread {
private km1Monitor theMonitor=new km1Monitor();;
private class Wolf extends Thread {
public void run() {
while(true) {
System.out.println("Wolf is trying to enter");
theMonitor.insertwolf();
try {
sleep(100);
}
catch (InterruptedException ex){}
}
}
}
private class Sheap extends Thread {
public void run() {
while(true) {
System.out.println("Sheap is trying to enter");
theMonitor.insertsheap();
try {
sleep(100);
}
catch (InterruptedException ex){}
}
}
}
public void run(){
Wolf aWolf=new Wolf();
Sheap aSheap=new Sheap();
aSheap.start();
aWolf.start();
while(true) {}
}
public static void main(String args[]) {
km1 holder = new km1();
holder.start();
}
}
Problem was in main method but i'm relativly new in java so these mistakes could happen.
with regardes
Michael
Michael Been
Greenhorn

Joined: Jan 15, 2003
Posts: 5
br_misionara it should be nmb_sheap,insertmisionara=insertsheap.
sorry i didn't edit well.
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
It�s really one-way handshake.
I read your code in a hurry( have sth to do now ), got a feeling there might be some problem in your monitor class. I�ll turn to this thread and read the code again when I get time.

Regards,
Ellen
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Modified code:

The monitor class here:

When the program ran, the output was not what I expected at all...Merely reading the code, I think the boat class cannot disdinguish which is a sheep thread and which is a wolf thread. What�s worse, the output shows that the BarrierBoat class actually has nothing to do with the transportation. Any idea helps out? Thanks very much in advance!

Regards,
Ellen
[ January 17, 2003: Message edited by: Ellen Fu ]
 
GeeCON Prague 2014
 
subject: Monitor in Java