+ Reply to Thread
Results 1 to 3 of 3

Thread: threads in java

  1. #1
    Newbie stasik is an unknown quantity at this point stasik's Avatar
    Join Date
    Mar 2007
    Posts
    4

    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. #2
    Newbie stasik is an unknown quantity at this point stasik's Avatar
    Join Date
    Mar 2007
    Posts
    4
    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.

  3. #3
    Newbie stasik is an unknown quantity at this point stasik's Avatar
    Join Date
    Mar 2007
    Posts
    4
    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);
    }

    }

    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. questions about java games
    By stack in forum Java Help
    Replies: 3
    Last Post: 07-02-2007, 05:35 PM
  2. MS-SQL deadlock and hang the Java application
    By reachpradeep in forum Database & Database Programming
    Replies: 1
    Last Post: 03-11-2007, 04:20 AM
  3. Java Facts
    By techni68 in forum Java Help
    Replies: 0
    Last Post: 01-17-2007, 01:41 PM
  4. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 03:05 PM
  5. Java Help Files
    By xXHalfSliceXx in forum Java Help
    Replies: 3
    Last Post: 11-28-2006, 11:30 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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