Closed Thread
Results 1 to 3 of 3

Thread: threads in java

  1. #1
    stasik's Avatar
    stasik is offline Newbie
    Join Date
    Mar 2007
    Posts
    4
    Rep Power
    0

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

    }

    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    stasik's Avatar
    stasik is offline Newbie
    Join Date
    Mar 2007
    Posts
    4
    Rep Power
    0
    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.

  4. #3
    stasik's Avatar
    stasik is offline Newbie
    Join Date
    Mar 2007
    Posts
    4
    Rep Power
    0
    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);
    }

    }

    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java threads, a little help please :)
    By dusch in forum Java Help
    Replies: 6
    Last Post: 10-30-2011, 10:01 AM
  2. Intermediate Java Threads
    By wim DC in forum Java Tutorials
    Replies: 6
    Last Post: 09-10-2011, 09:41 AM
  3. multi threads in java
    By eman ahmed in forum Java Help
    Replies: 1
    Last Post: 11-25-2010, 12:14 AM
  4. Tutorial: Java Threads
    By Jordan in forum Java Tutorials
    Replies: 1
    Last Post: 05-17-2008, 08:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts