Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Vote on your favorite hash algorithm here!

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-13-2007, 03:33 PM
stasik's Avatar   
stasik stasik is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Credits: 0
Rep Power: 0
stasik is on a distinguished road
Default threads in java

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);
}

}

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-13-2007, 03:34 PM
stasik's Avatar   
stasik stasik is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Credits: 0
Rep Power: 0
stasik is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-13-2007, 03:34 PM
stasik's Avatar   
stasik stasik is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Credits: 0
Rep Power: 0
stasik is on a distinguished road
Default

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);
}

}

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
questions about java games stack Java Help 3 07-02-2007 05:35 PM
MS-SQL deadlock and hang the Java application reachpradeep Database & Database Programming 1 03-11-2007 04:20 AM
Java Facts techni68 Java Help 0 01-17-2007 01:41 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM
Java Help Files xXHalfSliceXx Java Help 3 11-28-2006 11:30 PM


All times are GMT -5. The time now is 08:51 PM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1075.89
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 900.18
John ........ 890.77
Brandon W ........ 770.65
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 232.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads