Results 1 to 11 of 11

Thread: Threading

  1. #1
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Threading

    I need help with a homework assignment! I know some do not like to give help with homework but I have managed to do all of the work except one part. The project is to take user input to begin a requested number of threads. The program is then supposed to say "thread xxx of yyy says hello." I have the program complete to there it will begin the number of requested threads. Where I am having the issue is how to make the thread identify itself and of how many threads. I thought I could simply use the variables created in the main method but quickly realized that the main method is static and those variables are out of scope when I try to change them in the runnable. If someone could explain how I could do this and finish this project it would be greatly appericated. I have been out of coding for a few years and am very rusty.
    Thanks all!!!!

    Here is my code

    // File Name: HelloWorldThread.java
    // Written By: Michael R. Stewardson
    // Date 07 April 2010

    // Java program that creates a muli-threaded demonstration based on the number
    // of threads a user enters.

    import java.util.Scanner;

    public class HelloWorldThread implements Runnable
    {



    // Asks the user to enter a number of threads and then reports back to
    // the user informing the order in which the process completed by thread ID.

    public static void main (String[] args)
    {
    Scanner input = new Scanner(System.in);
    int numThreads,
    // Number of threads to run
    k,

    global;
    // Thread index
    Thread[]thr;
    // array of threads
    HelloWorldThread hello = new HelloWorldThread();
    // Runnable for each thread

    // Input the number of Threads
    System.out.print("Enter number of threads: ");
    numThreads = input.nextInt();
    global = numThreads;

    //Create the threads
    thr = new Thread[numThreads];
    for (k = 0; k < numThreads; k++)
    thr[k] = new Thread(hello);
    //Start the threads
    for(k = 0; k < numThreads; k++)
    thr[k].start();
    for (k = 0; k < numThreads; k++)
    {
    try
    {
    thr[k].join();
    } catch (InterruptedException ex)
    {
    System.out.println("Thread " + k + " interrupted.");
    }
    }
    }

    public void donno ()
    {

    System.out.println("Hello from thread number " + "" + " of " + 10);
    }

    public void run()
    {
    donno();
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    How would give a Person object a name?

  3. #3
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    I am really struggeling with this for some reason! It seems so easy and thats why I am so frusterated with it haha! I am really confused as to where code for a name would even go..... If its in the main method how can it be accessed by the run method? And if it belongs in the run method, how can I get the variable info from the main method which is static? Really confused!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You made the HelloWorldThread class implement Runnable. What is so hard with passing some values to its constructor?

  5. #5
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Im sorry.... I have been out of programming for years and this is my first class in years. I think I have an idea of what you mean but I am not 100% sure. I have added a line of code where I created the threads in the for loop which sets a var named thrName = k +1 would I pass that var name to the coustructor for the threads? I am proboly over thinking this....

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yes. You would also pass the total number of threads and not hard code it to be 10.

  7. #7
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok im dumb... heres what I have with the changes....
    Java Code:
    // File Name:	HelloWorldThread.java
    // Written By:	Michael R. Stewardson
    // Date			07 April 2010
    
    // Java program that creates a muli-threaded demonstration based on the number
    //	of threads a user enters.
    
    import java.util.Scanner;
    
    public class HelloWorldThread implements Runnable 
    {
    	
    	
    
    	// Asks the user to enter a number of threads and then reports back to 
    	// the user informing the order in which the process completed by thread ID.
    	
    	public static void main (String[] args) 
    	{
    	Scanner input = new Scanner(System.in);
    		int numThreads,
    			// Number of threads to run
    		k,
    		
    		global;
    			// Thread index
    		Thread[]thr;
    			// array of threads
    		HelloWorldThread hello = new HelloWorldThread(int thrName, int total);
    			// Runnable for each thread
    		
    		// Input the number of Threads
    		System.out.print("Enter number of threads: ");
    		numThreads = input.nextInt();
    		total = numThreads;
    		
    		//Create the threads
    		thr = new Thread[numThreads];
    		for (k = 0; k < numThreads; k++)
    		{
    			thr[k] = new Thread(hello);
    			thrName = k + 1;
    		}
    		//Start the threads
    		for(k = 0; k < numThreads; k++)
    			thr[k].start();
    		for (k = 0; k < numThreads; k++) 
    		{
    			try 
    			{
    				thr[k].join();
    			} 	catch (InterruptedException ex)
    			 	{
    				System.out.println("Thread " + k + " interrupted.");
    				}
    		}	
    	}
    	
    		public void donno () 
    		{
    
    			System.out.println("Hello from thread number " thrName + " of " + total);	
    		} 
    	
    		public void run() 
    		{
    			donno();
    		}
    }
    and here is the compiler error

    HelloWorldThread.java:29: '.class' expected
    HelloWorldThread hello = new HelloWorldThread(int thrName, int total);
    ^
    HelloWorldThread.java:29: <identifier> expected
    HelloWorldThread hello = new HelloWorldThread(int thrName, int total);
    ^
    HelloWorldThread.java:29: not a statement
    HelloWorldThread hello = new HelloWorldThread(int thrName, int total);
    ^
    HelloWorldThread.java:29: ';' expected
    HelloWorldThread hello = new HelloWorldThread(int thrName, int total);
    ^
    HelloWorldThread.java:62: ')' expected
    System.out.println("Hello from thread number " thrName + " of " + total);
    ^
    HelloWorldThread.java:62: not a statement
    System.out.println("Hello from thread number " thrName + " of " + total);
    ^
    HelloWorldThread.java:62: ';' expected
    System.out.println("Hello from thread number " thrName + " of " + total);
    ^
    7 errors

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You have to declare and initialise variables before you try to use them.

  9. #9
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    My bad.... ehh getting late.... I fixed that and a few other small things I noticed and not I get.....

    ----jGRASP exec: javac -g HelloWorldThread.java

    HelloWorldThread.java:31: cannot find symbol
    symbol : constructor HelloWorldThread(int,int)
    location: class HelloWorldThread
    HelloWorldThread hello = new HelloWorldThread(thrName, total);
    ^
    HelloWorldThread.java:65: cannot find symbol
    symbol : variable thrName
    location: class HelloWorldThread
    System.out.println("Hello from thread number " + thrName + " of " + total);
    ^
    HelloWorldThread.java:65: cannot find symbol
    symbol : variable total
    location: class HelloWorldThread
    System.out.println("Hello from thread number " + thrName + " of " + total);
    ^
    3 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You cannot call a constructors or methods that do not exist. Programming does not happen by magic.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I don't want to be rude here but your basic knowledge is severely lacking. Coming here with every little error you get and have someone explain it to you is not conducive to learning. You need to revise the basics. Read books and/or online tutorials to get a better understanding. When you do you should be able to fix 99% of these errors yourself.

Similar Threads

  1. Threading
    By kaptan_singh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2011, 09:23 PM
  2. threading help
    By the reporter in forum AWT / Swing
    Replies: 19
    Last Post: 06-21-2010, 06:50 PM
  3. Threading
    By hedonist in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2010, 03:21 PM
  4. need some help with threading
    By dinosoep in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 06:31 PM
  5. Threading
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 11:53 PM

Posting Permissions

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