Solved

Help with multithreading program

Posted on 2012-03-26
12
284 Views
Last Modified: 2012-08-14
Hi, I am trying to write a program for my class which I just about have worked out but can't get over the hump here.  I am supposed to write a program that utilizes mutlithreading and I have coded it so that I have three threads running and it deducts 1 from the total.  The 1st thread runs every 2 seconds, the 2nd thread runs every 3 seconds and the 3rd thread runs every 4 seconds (each deducting 1 from the total every interval).  Once the total reaches 0 it prints "I am guest X.  I ate XXX cookies."  My problem is that I can get it to print out two of the three guests which are guest 1 and 3 but 2 never prints and I am not sure why.  Any help would be much appreciated...thanks.

 
import java.util.Timer;
 import java.util.TimerTask;

 /**
  * Schedule a task that executes once every second.
  */

 public class Cookie {

   Timer timer;
   Timer timer2;
   Timer timer3;
   int Total = 100;

   public Cookie() {

     timer = new Timer();
     timer2 = new Timer();
     timer3 = new Timer();
     timer.schedule(new RemindTask(), 0, 2 * 1000);
     timer2.schedule(new RemindTask2(), 0, 3 * 1000);
     timer3.schedule(new RemindTask3(), 0, 4 * 1000);
   }

   class RemindTask extends TimerTask {
     int count = 0;

     public void run() {
       if (Total > 0) {
         count++;
         Total--;

       } else {

         System.out.println("I am guest 1.  I ate " + count + " cookies.");
         System.exit(0);
       }
     }

   }
class RemindTask2 extends TimerTask {
	      int count = 0;

	      public void run() {
	        if (Total > 0) {
	          count++;
	          Total--;

	        } else {

	          System.out.println("I am guest 2.  I ate " + count + " cookies.");
	          System.exit(0);
	        }
     }
 }
 class RemindTask3 extends TimerTask {
 	      int count = 0;

 	      public void run() {
 	        if (Total > 0) {
 	          count++;
 	          Total--;

 	        } else {

 	          System.out.println("I am guest 3.  I ate " + count + " cookies.");
 	          System.exit(0);
 	        }
     }
}

   public static void main(String args[]) {
     System.out.println("About to schedule task.");
     new Cookie();
     new Cookie();
     new Cookie();
     System.out.println("Task scheduled.");
   }
}

Open in new window

0
Comment
Question by:jwhmack
  • 7
  • 5
12 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
dont yuse System.exeit()
use timer.cancle()
and purge as below

See - I reduces number of cokkies to 20 to make it faster
import java.util.Timer;
 import java.util.TimerTask;

 /**
  * Schedule a task that executes once every second.
  */

 public class Cookie {

   Timer timer;
   Timer timer2;
   Timer timer3;
   int Total = 20;

   public Cookie() {

     timer = new Timer();
     timer2 = new Timer();
     timer3 = new Timer();
     timer.schedule(new RemindTask(), 0, 2 * 1000);
     timer2.schedule(new RemindTask2(), 0, 3 * 1000);
     timer3.schedule(new RemindTask3(), 0, 4 * 1000);
   }

   class RemindTask extends TimerTask {
     int count = 0;

     public void run() {
       if (Total > 0) {
         count++;
         Total--;
           System.out.println("task0: "  + Total);

       } else {

         System.out.println("I am guest 1.  I ate " + count + " cookies.");
        // System.exit(0);
           timer.cancel();
           timer.purge();

       }
     }

   }
class RemindTask2 extends TimerTask {
	      int count = 0;

	      public void run() {
	        if (Total > 0) {
	          count++;
	          Total--;
                  System.out.println("task2: "  + Total);

	        } else {

	          System.out.println("I am guest 2.  I ate " + count + " cookies.");
	        //  System.exit(0);
                  timer2.cancel();
           timer2.purge();
	        }
     }
 }
 class RemindTask3 extends TimerTask {
 	      int count = 0;

 	      public void run() {
 	        if (Total > 0) {
 	          count++;
 	          Total--;
                   System.out.println("task3: "  + Total);

 	        } else {

 	          System.out.println("I am guest 3.  I ate " + count + " cookies.");
 	        //  System.exit(0);
                   timer3.cancel();
           timer3.purge();
 	        }
     }
}

   public static void main(String args[]) {
     System.out.println("About to schedule task.");
     new Cookie();
    // new Cookie();
    // new Cookie();
     System.out.println("Task scheduled.");
   }
}

Open in new window



Output:

About to schedule task.
Task scheduled.
task2: 17
task0: 17
task3: 17
task0: 16
task2: 15
task3: 14
task0: 13
task2: 12
task0: 12
task3: 11
task0: 11
task2: 10
task0: 9
task3: 8
task0: 7
task2: 8
task0: 6
task2: 5
task0: 4
task3: 3
task0: 2
task2: 2
task0: 1
task3: 1
task2: 0
I am guest 1.  I ate 11 cookies.
I am guest 2.  I ate 8 cookies.
I am guest 3.  I ate 6 cookies.

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
You of course want seprate count variables for each guest
Otherwise it does not make much sense

import java.util.Timer;
 import java.util.TimerTask;

 /**
  * Schedule a task that executes once every second.
  */

 public class Cookie {

   Timer timer;
   Timer timer2;
   Timer timer3;
   int Total = 20;

     int count = 0;
            int count2 = 0;

            int count3 = 0;

   public Cookie() {

     timer = new Timer();
     timer2 = new Timer();
     timer3 = new Timer();
     timer.schedule(new RemindTask(), 0, 2 * 1000);
     timer2.schedule(new RemindTask2(), 0, 3 * 1000);
     timer3.schedule(new RemindTask3(), 0, 4 * 1000);
   }

   class RemindTask extends TimerTask {

     public void run() {
       if (Total > 0) {
         count++;
         Total--;
           System.out.println("task0: "  + Total);

       } else {

         System.out.println("I am guest 1.  I ate " + count + " cookies.");
        // System.exit(0);
           timer.cancel();
           timer.purge();

       }
     }

   }
class RemindTask2 extends TimerTask {
	      int count = 0;

	      public void run() {
	        if (Total > 0) {
	          count2++;
	          Total--;
                  System.out.println("task2: "  + Total);

	        } else {

	          System.out.println("I am guest 2.  I ate " + count2 + " cookies.");
	        //  System.exit(0);
                  timer2.cancel();
           timer2.purge();
	        }
     }
 }
 class RemindTask3 extends TimerTask {
 	      int count = 0;

 	      public void run() {
 	        if (Total > 0) {
 	          count3++;
 	          Total--;
                   System.out.println("task3: "  + Total);

 	        } else {

 	          System.out.println("I am guest 3.  I ate " + count3 + " cookies.");
 	        //  System.exit(0);
                   timer3.cancel();
           timer3.purge();
 	        }
     }
}

   public static void main(String args[]) {
     System.out.println("About to schedule task.");
     new Cookie();
    // new Cookie();
    // new Cookie();
     System.out.println("Task scheduled.");
   }
}

Open in new window



Output:

About to schedule task.
Task scheduled.
task0: 19
task2: 18
task3: 17
task0: 16
task2: 15
task0: 14
task3: 13
task0: 12
task2: 11
task0: 10
task3: 9
task2: 8
task0: 7
task0: 6
task3: 5
task2: 4
task0: 3
task2: 2
task0: 1
task3: 0
I am guest 1.  I ate 9 cookies.
I am guest 2.  I ate 6 cookies.
I am guest 3.  I ate 5 cookies.

Open in new window

0
 

Author Comment

by:jwhmack
Comment Utility
When I used that it just gave me an endless loop and never ended.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
No, your code as it was - worked for me and didn't do any endles loop - it just was reaching end and exiting - System.exit(0) just closes JVM - I was surpriised that it printed two - it should have printed one in my opinion, but maybe there was some time coincindecne.
But there was no endless loop - you just print on each stage ,as uit took long time and you saw nothing till it reached 100
In the end - no sense to use System.exit(0) in this case.
Now it works fine for me - chedck it.
0
 

Author Comment

by:jwhmack
Comment Utility
I know what my problem was.  I neglected to change the timerX.cancel() to its own instance for each if statement.  It worked better but the results were still not quite expected.  This is what was returned:
About to schedule task.
Task scheduled.
I am guest 1.  I ate 47 cookies.
I am guest 1.  I ate 48 cookies.
I am guest 3.  I ate 24 cookies.
I am guest 3.  I ate 24 cookies.
I am guest 2.  I ate 32 cookies.
I am guest 2.  I ate 32 cookies.
I am guest 2.  I ate 35 cookies.
I am guest 1.  I ate 53 cookies.
I am guest 3.  I ate 27 cookies.
Press any key to continue . . .

 import java.util.Timer;
 import java.util.TimerTask;

 /**
  * Schedule a task that executes once every second.
  */

 public class Cookie {

   Timer timer;
   Timer timer2;
   Timer timer3;
   int count=0,count2=0,count3=0;
   int Total = 100;

   public Cookie() {

     timer = new Timer();
     timer2 = new Timer();
     timer3 = new Timer();
     timer.schedule(new RemindTask(), 0, 2 * 1000);
     timer2.schedule(new RemindTask2(), 0, 3 * 1000);
     timer3.schedule(new RemindTask3(), 0, 4 * 1000);
   }

   class RemindTask extends TimerTask {

     public void run() {
       if (Total > 0) {
         count++;
         Total--;

       } else {

         System.out.println("I am guest 1.  I ate " + count + " cookies.");
         timer.cancel();
		 timer.purge();

       }
     }

   }
class RemindTask2 extends TimerTask {

	      public void run() {
	        if (Total > 0) {
	          count2++;
	          Total--;

	        } else {

	          System.out.println("I am guest 2.  I ate " + count2 + " cookies.");
	          timer2.cancel();
			  timer2.purge();

	        }
     }
 }
 class RemindTask3 extends TimerTask {

 	      public void run() {
 	        if (Total > 0) {
 	          count3++;
 	          Total--;

 	        } else {

 	          System.out.println("I am guest 3.  I ate " + count3 + " cookies.");
 	          timer3.cancel();
			  timer3.purge();

 	        }
     }
}

   public static void main(String args[]) {
     System.out.println("About to schedule task.");
     new Cookie();
     new Cookie();
     new Cookie();
     System.out.println("Task scheduled.");
   }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
I din't sse any timerX.cancel()  in your original code - I added those instead of the Systemn.exit(0)

There is also no need to start Cooke() three times in the main()
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:jwhmack
Comment Utility
Wow, didn't catch that at the end, that helped but my results added up to 118 instead of just 100...do you know why that would be?

About to schedule task.
Task scheduled.
I am guest 2.  I ate 36 cookies.
I am guest 3.  I ate 27 cookies.
I am guest 1.  I ate 55 cookies.
Press any key to continue . . .
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
I don't know which code you are using. use exectly the code I posted above (and below)
and you see - I got exactly sum of 20 cookies when I modified the counts specifically for each ttherad;

import java.util.Timer;
 import java.util.TimerTask;

 /**
  * Schedule a task that executes once every second.
  */

 public class Cookie {

   Timer timer;
   Timer timer2;
   Timer timer3;
   int Total = 20;

     int count = 0;
            int count2 = 0;

            int count3 = 0;

   public Cookie() {

     timer = new Timer();
     timer2 = new Timer();
     timer3 = new Timer();
     timer.schedule(new RemindTask(), 0, 2 * 1000);
     timer2.schedule(new RemindTask2(), 0, 3 * 1000);
     timer3.schedule(new RemindTask3(), 0, 4 * 1000);
   }

   class RemindTask extends TimerTask {

     public void run() {
       if (Total > 0) {
         count++;
         Total--;
           System.out.println("task0: "  + Total);

       } else {

         System.out.println("I am guest 1.  I ate " + count + " cookies.");
        // System.exit(0);
           timer.cancel();
           timer.purge();

       }
     }

   }
class RemindTask2 extends TimerTask {
	      int count = 0;

	      public void run() {
	        if (Total > 0) {
	          count2++;
	          Total--;
                  System.out.println("task2: "  + Total);

	        } else {

	          System.out.println("I am guest 2.  I ate " + count2 + " cookies.");
	        //  System.exit(0);
                  timer2.cancel();
           timer2.purge();
	        }
     }
 }
 class RemindTask3 extends TimerTask {
 	      int count = 0;

 	      public void run() {
 	        if (Total > 0) {
 	          count3++;
 	          Total--;
                   System.out.println("task3: "  + Total);

 	        } else {

 	          System.out.println("I am guest 3.  I ate " + count3 + " cookies.");
 	        //  System.exit(0);
                   timer3.cancel();
           timer3.purge();
 	        }
     }
}

   public static void main(String args[]) {
     System.out.println("About to schedule task.");
     new Cookie();
    // new Cookie();
    // new Cookie();
     System.out.println("Task scheduled.");
   }
}
                                            

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
No, but actually it may happen I think because you enedd to somehow synchronize Total
otherwise it may happen that you would subtract twice the same cookie
0
 

Author Comment

by:jwhmack
Comment Utility
I copied and pasted your code and I returned a result of 22:

About to schedule task.
task0: 19
task2: 18
Task scheduled.
task3: 17
task0: 16
task2: 15
task0: 14
task3: 13
task2: 12
task0: 12
task0: 11
task3: 10
task2: 9
task0: 8
task0: 6
task2: 6
task3: 5
task0: 4
task2: 3
task0: 2
task3: 1
task2: 0
task0: 0
I am guest 1.  I ate 10 cookies.
I am guest 3.  I ate 5 cookies.
I am guest 2.  I ate 7 cookies.
Press any key to continue . . .
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
try this, with this I hope it would be alwyas total number:
import java.util.Timer;
 import java.util.TimerTask;

 /**
  * Schedule a task that executes once every second.
  */

 public class Cookie {

   Timer timer;
   Timer timer2;
   Timer timer3;
   int Total = 50;

     int count = 0;
            int count2 = 0;

            int count3 = 0;

   public Cookie() {

     timer = new Timer();
     timer2 = new Timer();
     timer3 = new Timer();
     timer.schedule(new RemindTask(), 0, 2 * 1000);
     timer2.schedule(new RemindTask2(), 0, 3 * 1000);
     timer3.schedule(new RemindTask3(), 0, 4 * 1000);
   }

      public synchronized void decrementTotal(){
          Total--;

  }


   class RemindTask extends TimerTask {



     public void run() {
       if (Total > 0) {
         count++;
         decrementTotal();
           System.out.println("task0: "  + Total);

       } else {

         System.out.println("I am guest 1.  I ate " + count + " cookies.");
        // System.exit(0);
           timer.cancel();
           timer.purge();

       }
     }

   }
class RemindTask2 extends TimerTask {
	      int count = 0;

	      public void run() {
	        if (Total > 0) {
	          count2++;
	         decrementTotal();
                  System.out.println("task2: "  + Total);

	        } else {

	          System.out.println("I am guest 2.  I ate " + count2 + " cookies.");
	        //  System.exit(0);
                  timer2.cancel();
           timer2.purge();
	        }
     }
 }
 class RemindTask3 extends TimerTask {
 	      int count = 0;

 	      public void run() {
 	        if (Total > 0) {
 	          count3++;
 	         decrementTotal();
                   System.out.println("task3: "  + Total);

 	        } else {

 	          System.out.println("I am guest 3.  I ate " + count3 + " cookies.");
 	        //  System.exit(0);
                   timer3.cancel();
           timer3.purge();
 	        }
     }
}

   public static void main(String args[]) {
     System.out.println("About to schedule task.");
     new Cookie();
    // new Cookie();
    // new Cookie();
     System.out.println("Task scheduled.");
   }
}

Open in new window

0
 

Author Closing Comment

by:jwhmack
Comment Utility
That did the trick.  I looked up the line of code you added and I see that this makes it so a thread can only use that total calculation one at a time whereas and when I compare the examples of when I ran the program before and after the total would sometimes be the same by two threads and after it was not.  Thanks so much for your help.  You work so hard on a problem for so long and bang your head on the desk eventually younothing you do looks right anymore.  That's where I was at when I posted...nothing I was doing was working so I so appreciate your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now