Results 1 to 8 of 8
Thread: My daily question(Threading)
- 05-03-2012, 03:25 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
My daily question(Threading)
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: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(); } }
[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?
- 05-03-2012, 03:34 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 05-03-2012, 03:40 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: My daily question(Threading)
- 05-03-2012, 04:18 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: My daily question(Threading)
This line here creates an EHU3 object using this constructor:Java Code:new EHU3(95, 23).start();
In which you don't set 'n'.Java Code:public EHU3(int a, int b){ this.object1 = a; this.object2 = b; }
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.
- 05-03-2012, 04:47 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
- 05-03-2012, 05:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,372
- Blog Entries
- 7
- Rep Power
- 17
Re: My daily question(Threading)
What is supposed to call the run( ... ) method that has two parameters?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-03-2012, 05:51 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: My daily question(Threading)
In order for n to be set to a value that is not 0, you must call your constructor that sets n:
When you create your first thread you call the constructor that sets a and b, but leaves n at 0.Java Code:public EHU3(int n){ this.n = n; }
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.
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.
this line creates a new thread where:Java Code:new EHU3(95, 23).start();
a = 95
b = 23
and since n is not set its default value is 0
this line creates a new thread where:Java Code:new EHU3(1).start();
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:
These variables are currently unused.Java Code:int object1; int object2; Thread t = new Thread();
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:
you must call it from elsewhere in the code because it is not executed with the call to start().Java Code:public void run(int a, int b)
Last edited by brynpttrsn; 05-03-2012 at 06:04 PM.
- 05-04-2012, 05:06 PM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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
Is to just add, subtract, multiply, divide, a and b. And do it in intervals of 2000.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."); } }
Similar Threads
-
Simple question about multi-threading
By intrepid604 in forum New To JavaReplies: 0Last Post: 03-07-2011, 10:20 PM -
Need Help! Multi-Threading question!
By pinkette in forum New To JavaReplies: 8Last Post: 01-13-2011, 07:08 PM -
threading execution time question
By centenial in forum Threads and SynchronizationReplies: 4Last Post: 09-09-2010, 05:49 AM -
question about Multi threading in Java
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:55 AM -
Connect Daily Web Calendar 3.3.4
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-19-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks