|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||||
|
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); } } } |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |
Goal: 100,000 Posts
Complete: 83%