Results 1 to 8 of 8
Thread: Threads
- 05-01-2012, 04:13 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Threads
Single thread means just a normal program right? It's just normal. ie
But multiple is different how?Java Code:class HelloWorld{ public static void main(String [] args){ System.out.println("hi"); } }
Is this an example of multiplethreading?
Java Code:public class Threading extends Thread { public void run(){ for(int i = 0; i <= 30; i++){ System.out.println(i); } } public static void main(String [] args){ new Threading().start(); } }Last edited by Army; 05-01-2012 at 04:19 PM.
- 05-01-2012, 04:21 PM #2
Re: Threads
This is the first result for googling "java threading": Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-01-2012, 04:24 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Threads
I know, I was already looking at it. Is there any difference in the first one and the second one that i posted?
- 05-01-2012, 04:31 PM #4
Re: Threads
What happens when you run each? To further demonstrate threading, you might try something more like this (not tested, may contain compiler errors):
Java Code:public class Threading extends Thread { int n; public Threading(int n){ this.n = n; } public void run(){ for(int i = 0; i <= 1000; i++){ System.out.println("Thread " + n + " executing."); System.out.println(i); System.out.println("Thread " + n + " done executing. Sleeping now..."); Thread.sleep(Math.random() * 5000); } } public static void main(String [] args){ new Threading(1).start(); new Threading(2).start(); new Threading(3).start(); } }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-01-2012, 04:39 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Threads
Write a program to print the even numbers and the odd numbers between 0 and 30 using a single thread and then again using multiple threads.
^That is the question I was asked. The second code is the one I thought had implemented multi-threading.
- 05-01-2012, 04:47 PM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: Threads
After some tweaking I did this, but then it does 3 threads all printing the same thing. How would I start to make it where as if one thread is printing the same number then the thread with the same number would sleep.Java Code:public class Threading extends Thread { int n; public Threading(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 + " done starting. Sleeping now..."); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("Thread " + n + "was not finished."); } } } public static void main(String [] args){ new Threading(1).start(); new Threading(2).start(); new Threading(3).start(); } }
- 05-01-2012, 04:49 PM #7
Re: Threads
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-01-2012, 04:52 PM #8
Re: Threads
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Threads per Connection or Threads per Request
By Felic in forum New To JavaReplies: 4Last Post: 11-22-2011, 09:15 PM -
Help with Threads
By gicp89 in forum Threads and SynchronizationReplies: 1Last Post: 11-01-2011, 09:00 PM -
Threads and UML
By JUser in forum Advanced JavaReplies: 0Last Post: 09-27-2010, 08:43 PM -
GUI and Threads
By rp181 in forum Threads and SynchronizationReplies: 1Last Post: 10-10-2009, 08:39 PM -
Threads!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-04-2008, 04:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks