[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help with multithreading program

Posted on 2012-03-26
12
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37767108
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
ID: 37767143
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
ID: 37767192
When I used that it just gave me an endless loop and never ended.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37767211
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
ID: 37767258
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
ID: 37767271
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
 

Author Comment

by:jwhmack
ID: 37767288
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
ID: 37767302
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
ID: 37767322
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
ID: 37767324
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 2000 total points
ID: 37767339
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
ID: 37767385
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

649 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