Need help with runnable interface and threads

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3513 Views - Last Post: 19 July 2012 - 08:06 AM Rate Topic: -----

#1 Redline5th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-July 12

Need help with runnable interface and threads

Posted 18 July 2012 - 09:34 PM

Hello all!

I am in need of help with a Java class project. The program is not difficult at all but our book doesn't cover the subject matter at hand very well at all. I've been researching a lot and have a better book on the way from Amazon. Now... I know the program has the potential to work as it sort of does, just not exactly the way it needs to.

The output that I get is that my countdown "timer" is repeated 5 times.

This means that it goes 5,4,3,2,1 BLAST OFF! exactly five times... it needs to happen once. Everything seems to happen out of sequence.

With each repitition I get one of my threads printed and displayed.

It needs to do the countdown 1 time and then display my 5 thread names and start them. That's all this program has to do.

Any help would be greatly appreciated! I'm very new to programming.

package threadcount;

public class ThreadCount implements Runnable {

    private int ignitionTimer = 5;
    private static int threadCount = 0;
    private int numThreads = ++threadCount;

    public void run() {
        while(true) {

            System.out.println("Countdown = " + ignitionTimer);
              
            if (--ignitionTimer == 0) {
                
            System.out.println("\nBlast Off!");
                
                return;
            }
        }
    }

    public static void main(String[] args)
        throws java.lang.InterruptedException {
       
        for (int i = 0; i < 5; i++) {
            Runnable tv = new ThreadCount();
            Thread.sleep(1000);
            Thread thrd = new Thread(tv);
            thrd.start();
            System.out.println(thrd.getName()+" is starting...");
        }
        
        System.out.println("\nThreads have now been started\n");
    }        

}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with runnable interface and threads

#2 Redline5th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-July 12

Re: Need help with runnable interface and threads

Posted 18 July 2012 - 09:45 PM

Hello all!

I am looking to get some help with a simple multithreaded application for a school project. I am new to programming but am learning.

Here's the issue:

My application repeats the countdown 5,4,3,2,1, BLAST OFF! 5 times.... in-between these it starts and displays 1 of the 5 threads. Somehow the conntdown is associated with my loop.

The application should do the countdown 1 time and then list my threads by name and start them, then give a simple message at the end saying the threads are started. My application does work but it doesn't execute in order and the countdown shouldn't happen 5 different times.

Pretty simple. My application below works, just not in order and the countdown loops 5 different times instead of just going once... I even tried using the thread.sleep to see if it made an effect.

Any help is GREATLY appreciated!


package threadcount;

public class ThreadCount implements Runnable {

    private int ignitionTimer = 5;
    private static int threadCount = 0;
    private int numThreads = ++threadCount;

    public void run() {
        while(true) {

            System.out.println("Countdown = " + ignitionTimer);
            
            if (--ignitionTimer == 0) {

                
            System.out.println("\nBlast Off!");
                
                return;
            }
        }
    }

    public static void main(String[] args)
        throws java.lang.InterruptedException {
       
        for (int i = 0; i < 5; i++) {
            Runnable tv = new ThreadCount();
            Thread.sleep(1000);
            Thread thrd = new Thread(tv);
            thrd.start();
            System.out.println(thrd.getName()+" is starting...");
        }
        
        System.out.println("\nThreads have now been started\n");
    }        

}


Was This Post Helpful? 0
  • +
  • -

#3 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

Re: Need help with runnable interface and threads

Posted 18 July 2012 - 09:50 PM

for loops are running the loops over and over again, so it'll wait for the sleep to finish before looping it again. besides what IS your "order"?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Need help with runnable interface and threads

Posted 18 July 2012 - 09:52 PM

Moved from Java Tutorials to Java Help. the tutorials section is meant for tutorials only and not help requests. Thanks.

AAAND Duplicate topics merged.
Was This Post Helpful? 0
  • +
  • -

#5 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

Re: Need help with runnable interface and threads

Posted 18 July 2012 - 09:57 PM

Sry i skimmed it over so those might not make some sense.

Anyways what do you mean by going different times instead all at once?

and you should put the decrement outside of the if () bracket,
        while(true) {

            System.out.println("Countdown = " + ignitionTimer);
            ignitionTimes--;
            if (ignitionTimer == 0) {

                
            System.out.println("\nBlast Off!");
                
                return;
            }



Something like that, lol i'm a bit sleepy right now..
Was This Post Helpful? 0
  • +
  • -

#6 Redline5th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-July 12

Re: Need help with runnable interface and threads

Posted 18 July 2012 - 10:13 PM

View PostSilverEleak, on 18 July 2012 - 09:57 PM, said:

Sry i skimmed it over so those might not make some sense.

Anyways what do you mean by going different times instead all at once?

and you should put the decrement outside of the if () bracket,
        while(true) {

            System.out.println("Countdown = " + ignitionTimer);
            ignitionTimes--;
            if (ignitionTimer == 0) {

                
            System.out.println("\nBlast Off!");
                
                return;
            }



Something like that, lol i'm a bit sleepy right now..


Thanks!

The loop only encapsulates the thread display portion, not the countdown, so I can't Figure out why the countdown runs 5 times. Does the loop re run the entire code?

Also the order should be:

Count down ie: 5,4,3,2,1 blast off

And then

Thread1-5 is printed as well as started.

Pretty easy. The code output is all messed up.

Not only does the countdown display 5 seperate times, but the thread display horns in between the countdown.
Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 01:02 AM

It's printed 5 times becasue you have a for loop 0 to 5. Also, its a badly designed code as far as im concerned. All the threads have access to the same variables and no synchronization is put in place.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 04:09 AM

Why are you using 5 threads (well, 6 + system threads, actually) to do one thing, a single countdown from 5 to Blast Off?

Your program has the elements necessary to use the main thread to accomplish the countdown, but I think you've created additional threads because you're supposed to, but I'm not sure what the additional threads would do. Simply using the main thread to accomplish the task, you'd do something like:
public static void main( String[] args ) 
	throws InterruptedException 
{  
	for ( int i = 5 ; i > 0 ; i-- )
	{  
		// print the countdown
		System.out.print( i + " . . " );
		
		// pause the main thread for 1 sec.
		Thread.sleep(1000);  
	}  
	// finish the countdown with a big send off
	System.out.println( "Blast Off!" );
}

What were you thinking the other 5 threads would do?
Was This Post Helpful? 0
  • +
  • -

#9 Redline5th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-July 12

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 06:48 AM

View PostGregBrannon, on 19 July 2012 - 04:09 AM, said:

Why are you using 5 threads (well, 6 + system threads, actually) to do one thing, a single countdown from 5 to Blast Off?

Your program has the elements necessary to use the main thread to accomplish the countdown, but I think you've created additional threads because you're supposed to, but I'm not sure what the additional threads would do. Simply using the main thread to accomplish the task, you'd do something like:
public static void main( String[] args ) 
	throws InterruptedException 
{  
	for ( int i = 5 ; i > 0 ; i-- )
	{  
		// print the countdown
		System.out.print( i + " . . " );
		
		// pause the main thread for 1 sec.
		Thread.sleep(1000);  
	}  
	// finish the countdown with a big send off
	System.out.println( "Blast Off!" );
}

What were you thinking the other 5 threads would do?



Greg:

Part 2 of the assignment was to create, start, and display 5 threads.
Was This Post Helpful? 0
  • +
  • -

#10 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 07:08 AM

Whats your full assignment.
Was This Post Helpful? 0
  • +
  • -

#11 Redline5th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-July 12

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 07:33 AM

Attached is all I get for the assignment:

1. Implements the runnable() interface
2. Implements the run() method of the runnable() interface to do the following:
a. Count down, starting with the number 5 and going to 0
b. Print the name of the thread and the current value of countdown integer for each integer counted down
c. Print “Blast Off!” when 0 is reached in the countdown
3. Creates five threads in your main class giving each thread a name: “Thread 1”, “Thread 2”, “Thread 3”, “Thread 4”, and “Thread 5”
4. Starts all five threads

Granted, I know that this project doesn't make any sense at all to you guys, as it isn't a "practical" app and doesn't really serve a purpose.

All I have to do is create it. Now I know that I am not too far off.
Was This Post Helpful? 0
  • +
  • -

#12 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 07:37 AM

Your code looks fine then. It's meant to be printed 5 times.
Was This Post Helpful? 0
  • +
  • -

#13 Redline5th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 07-July 12

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 07:43 AM

Somehow I don't think the countdown is supposed to be printed 5 times. But maybe I'm wrong.
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 08:03 AM

If there are 5 threads doing the same run() method all running at the same time, then the countdown will HAVE to occur 5 times. What this will show, in addition to the obvious mechanics of building the program and getting it to run, is the unpredictability and variability of the actual execution of the threads.
Was This Post Helpful? 0
  • +
  • -

#15 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Need help with runnable interface and threads

Posted 19 July 2012 - 08:04 AM

Well.. not my assignment.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2