i have a code and want to make threads to work mutually,without using semaphores,just key words(wait,notify).this code implements four classes: Bag, Parent, Child and Sweets. Sweets creates an instance of Bag which is shared by the two threads, Parent and Child. The Parent continuously puts sweets into the bag while the Child continuously takes sweets out of the bag. As it stands the code will run but produces some rather strange results. One might expect the number of sweets in the bag to always equal the number put in minus the number taken out, i.e., the following should hold:
sweets = in - out, or,
delta = in - out - sweets = 0
Currently the code prints out the value of delta every two seconds. It does not remain zero.
// The bag that holds the sweets
class Bag {
// Variables
public int sweets, in, out;
// Constructor
public Bag() {
sweets = in = out = 0;
}
}
// The parent thread
class Parent extends Thread {
// Our thread works on this
public Bag bag;
// Constructor
public Parent(Bag bag) {
this.bag = bag;
}
// What our thread does
public void run() {
while (true) {
bag.sweets++;
bag.in++;
}
}
}
// The child thread
class Child extends Thread {
// Our thread works on this
public Bag bag;
// Constructor
public Child(Bag bag) {
this.bag = bag;
}
// What our thread does
public void run() {
while (true) {
bag.sweets--;
bag.out++;
}
}
}
// Main
class Sweets {
public static void main(String[] args) throws InterruptedException {
// Create our bag
Bag bag = new Bag();
// Create our child thread
Child cthread = new Child(bag);
// Create our parent thread
Parent pthread = new Parent(bag);
// Start threads
cthread.start();
pthread.start();
// Keep track
while (true) {
// Sleep for two seconds
Thread.sleep(2000);
// Display the current state of affairs
System.out.println("Delta = " + (bag.in - bag.out - bag.sweets)
+ " Sweets = " + bag.sweets);
}
}
}
the output should be:
Delta = 0 Sweets = 3968
Delta = 0 Sweets = 3643
Delta = 0 Sweets = 1871
Delta = 0 Sweets = 3065
Delta = 0 Sweets = 2779
Delta = 0 Sweets = 4955
Delta = 0 Sweets = 1022
Delta = 0 Sweets = 1065
Delta = 0 Sweets = 4959
Delta = 0 Sweets = 4965
Delta = 0 Sweets = 4823
Delta = 0 Sweets = 1002
Delta = 0 Sweets = 3272
Delta = 0 Sweets = 4953
Delta = 0 Sweets = 2801
Delta = 0 Sweets = 4643
Delta = 0 Sweets = 2554
etc.
i managed to make the threads work,but the bag.sweets seems not to be changing.and another thing,once the limit is reached(1000 or 5000),its not changing any more.
// The bag that holds the sweets
class Bag {
// Variables
public int sweets, in, out;
public boolean occupied;
// Constructor
public Bag() {
sweets = 2000;
in = out =2000;
occupied = false;
}
}//bag
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++
// The parent thread
class Parent extends Thread {
// Our thread works on this
public Bag bag;
// Constructor
public Parent(Bag bag) {
this.bag = bag;
}
// What our thread does
public synchronized void run() {
while (bag.occupied==true && bag.sweets>=5000) {
try {
wait();
} catch (InterruptedException e) { }
}
while(bag.sweets<5000){
bag.sweets=bag.sweets+1;
bag.in++;
System.out.print("\nadd");
}
bag.occupied=true;
notifyAll();
}
}//parent
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++
// The child thread
class Child extends Thread {
//boolean occupied;
// Our thread works on this
public Bag bag;
// Constructor
public Child(Bag bag) {
this.bag = bag;
}
// What our thread does
public synchronized void run() {
while (bag.occupied==false && bag.sweets<=1000){
try {
wait();
} catch (InterruptedException e) { }
}
while(bag.sweets>1000)
{
bag.sweets=bag.sweets-1;
bag.out++;
System.out.print("\nsub");
}
bag.occupied = false;
notifyAll();
}
}//child
//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++
// Main
class Sweets {
public static void main(String[] args) throws InterruptedException {
// Create our bag
Bag bag = new Bag();
// Create our child thread
Child cthread = new Child(bag);
// Create our parent thread
Parent pthread = new Parent(bag);
// Start threads
cthread.start();
pthread.start();
// Keep track
while (true) {
// Sleep for two seconds
Thread.sleep(20);
// Display the current state of affairs
System.out.println("Delta = " + (bag.in - bag.out - bag.sweets) + " Sweets = " + bag.sweets);
}
}
}
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks