Results 1 to 8 of 8
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default My daily question(Threading)

    Java Code:
    public class EHU3 extends Thread {
        int n;
        int object1;
        int object2;
        public EHU3(int n){
            this.n = n;
        }
     
        public void run(){
            for(int i = 0; i <= 30; i++){
                System.out.println("Thread " + n + " starting.");
                System.out.println(i);
                System.out.println("Thread " + n + " finished. Sleeping now...");
                try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				
    				System.out.println("Thread " + n + "was not finished.");
    			}
            }
        }
        public EHU3(int a, int b){
        	this.object1 = a;
        	this.object2 = b;
        }
        public void run(int a, int b){
        	try{
        		System.out.println("A plus B: " + a + b);
        		System.out.printf("A minus B: " , a - b);
        		System.out.println("A divided by B: " + a/b);
        		System.out.println("A times B: " + a*b);
        		Thread.sleep(2000);
        		
        	}catch(InterruptedException e){
        		System.out.println("Thread" + a + "was not finished");
        		System.out.println("Thread" + b + "was not finished");
        	}
        }
        public static void main(String [] args){
        	Thread t = new Thread();
        	new EHU3(95, 23).start();
            new EHU3(1).start();
            new EHU3(2).start();
            new EHU3(3).start();
            
        }
    }
    I initially wanted to do it where it would go from 1 - 30, with 3 threads, then when it finished add ab and such.... But all it has done is create another thread named 0. I don't even have a thread 0 on there, so I have no idea what happened. this is what I get:
    [spoiler]Thread 0 starting.
    0
    Thread 0 finished. Sleeping now...
    Thread 1 starting.
    0
    Thread 1 finished. Sleeping now...
    Thread 2 starting.
    0
    Thread 2 finished. Sleeping now...
    Thread 3 starting.
    0
    Thread 3 finished. Sleeping now...
    ETC...

    There is obviously no addition or sub, or mult, or division in there... wtf is up?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: My daily question(Threading)

    Yes you do.
    Look at the EHU3 constructor that takes two arguments.
    For the Thread you create via that constructor, what will the value of 'n' be?
    Please do not ask for code as refusal often offends.

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: My daily question(Threading)

    Quote Originally Posted by Tolls View Post
    Yes you do.
    Look at the EHU3 constructor that takes two arguments.
    For the Thread you create via that constructor, what will the value of 'n' be?
    Did I not overload the classes properly (not sure if overloading is the correct term since there is so much lingo in Java)? I just made the same things, but set it with different arguments...

    And is that like a trick question or something...?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: My daily question(Threading)

    Java Code:
            new EHU3(95, 23).start();
    This line here creates an EHU3 object using this constructor:
    Java Code:
        public EHU3(int a, int b){
             this.object1 = a;
             this.object2 = b;
         }
    In which you don't set 'n'.
    Consequently 'n' for this object has the default value of 0.
    It wasn't supposed to be a trick question.
    Please do not ask for code as refusal often offends.

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: My daily question(Threading)

    Quote Originally Posted by Tolls View Post
    Java Code:
            new EHU3(95, 23).start();
    This line here creates an EHU3 object using this constructor:
    Java Code:
        public EHU3(int a, int b){
             this.object1 = a;
             this.object2 = b;
         }
    In which you don't set 'n'.
    Consequently 'n' for this object has the default value of 0.
    It wasn't supposed to be a trick question.
    Would it not be impossible for me to create the parameter 'n' since there are two arguments for the method?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: My daily question(Threading)

    What is supposed to call the run( ... ) method that has two parameters?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: My daily question(Threading)

    Quote Originally Posted by Army View Post
    Would it not be impossible for me to create the parameter 'n' since there are two arguments for the method?
    In order for n to be set to a value that is not 0, you must call your constructor that sets n:
    Java Code:
    public EHU3(int n){
        this.n = n;
    }
    When you create your first thread you call the constructor that sets a and b, but leaves n at 0.
    If you wish to set n in the same constructor you would only have to add another parameter to set n as well as a and b.

    Quote Originally Posted by Army View Post
    with 3 threads
    In your main function you are creating 4 separate threads. Keep in mind that if you create "new" threads, all of their variables are separate from each other. The values of a and b are not set in the last three threads created.

    Java Code:
    new EHU3(95, 23).start();
    this line creates a new thread where:
    a = 95
    b = 23
    and since n is not set its default value is 0

    Java Code:
     new EHU3(1).start();
    this line creates a new thread where:
    a and b are not set so their default values are 0
    n = 1

    These are 2 completely different instances of EHU3 that have their own values for each variable.

    Also note that:
    Java Code:
    int object1;
    int object2;
    Thread t = new Thread();
    These variables are currently unused.
    Also:
    (as JosAH is pointing out)
    The way that a thread works does not allow you to overload the run method with additional parameters.
    If you wish to use this method:
    Java Code:
    public void run(int a, int b)
    you must call it from elsewhere in the code because it is not executed with the call to start().
    Last edited by brynpttrsn; 05-03-2012 at 06:04 PM.

  8. #8
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: My daily question(Threading)

    I guess you don't understand what I'm trying to do. Also I'm guessing that the code that I have only works for for loops because nothing is working. All I want this code
    Java Code:
     public EHU3(int n, int a, int b){
        	this.n = n;
        }
        public void run(int n, int n1){
        	System.out.println("A plus B: " + n + n1);
        	System.out.printf("A minus B: " , n - n1);
        	System.out.println("A divided by B: " + n/n1);
        	System.out.println("A times B: " + n*n1);
        	try{
        		
        		Thread.sleep(2000);
        		
        	}catch(InterruptedException e){
        		System.out.println("Thread was not terminated.");
        	}
        }
    Is to just add, subtract, multiply, divide, a and b. And do it in intervals of 2000.

Similar Threads

  1. Simple question about multi-threading
    By intrepid604 in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 10:20 PM
  2. Need Help! Multi-Threading question!
    By pinkette in forum New To Java
    Replies: 8
    Last Post: 01-13-2011, 07:08 PM
  3. threading execution time question
    By centenial in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-09-2010, 05:49 AM
  4. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 AM
  5. Connect Daily Web Calendar 3.3.4
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:27 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
  •