Results 1 to 10 of 10
- 11-07-2011, 01:40 PM #1
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
How to run two methords at the same time
Hello,
Im new to Java multithreading and have been expermenting. Ive came up with the following code which is very simple. What i would like to do is create two syncorized methords that print out some text. I would like to call these methords on differnet thread. Can anyone show me on how to do this?
Java Code:import java.lang.Thread; import java.lang.Runnable; import java.lang.InterruptedException; /** * * @author */ public class Threads implements Runnable { String name; Thread t; Threads(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); } public void run() { try { for (int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(12000); } }catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { Threads ob1 = new Threads("One"); Threads ob2 = new Threads("Two"); Threads ob3 = new Threads("Three"); System.out.println("Thread " + ob1.name + " is alive: " + ob1.t.isAlive()); System.out.println("Thread " + ob2.name + " is alive: " + ob2.t.isAlive()); System.out.println("Thread " + ob3.name + " is alive: " + ob3.t.isAlive()); try { System.out.println("Waiting for Threads to finish"); ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + ob1.name + " is alive: " + ob1.t.isAlive()); System.out.println("Thread " + ob2.name + " is alive: " + ob2.t.isAlive()); System.out.println("Thread " + ob3.name + " is alive: " + ob3.t.isAlive()); System.out.println("Main Thread Exiting"); } }
- 11-07-2011, 02:02 PM #2
Re: How to run two methords at the same time
Sooo just call the method from the Threads' run method, right?
Also, you can't run synchronized methods at the same time. That's a contradiction.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-07-2011, 02:51 PM #3
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
Re: How to run two methords at the same time
Hi KevinWorkman
I've modified the code to call two methords that just print to the screen. As you can see here i have two threads called ob1 and ob2. How can i get ob1 to call methord Output1 and ob2 to call methord Output2 thus each thread executing a differnet methord.
OUTPUTJava Code:package threads; import java.lang.Thread; import java.lang.Runnable; import java.lang.InterruptedException; /** * * @author steven.haynes */ public class Threads implements Runnable { String name; Thread thread; boolean available = true; Threads(String threadname) { name = threadname; thread = new Thread(this, name); System.out.println("New thread: " + thread); thread.start(); } public synchronized void Output1() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = true; notifyAll(); System.out.println("Output 1"); } public synchronized void Output2() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); System.out.println("Output 2"); } public void run() { Output1(); Output2(); } public static void main(String[] args) { Threads ob1 = new Threads("One"); Threads ob2 = new Threads("Two"); System.out.println("Thread " + ob1.name + " is alive: " + ob1.thread.isAlive()); System.out.println("Thread " + ob2.name + " is alive: " + ob2.thread.isAlive()); try { System.out.println("Waiting for Threads to finish"); ob1.thread.join(); ob2.thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + ob1.name + " is alive: " + ob1.thread.isAlive()); System.out.println("Thread " + ob2.name + " is alive: " + ob2.thread.isAlive()); System.out.println("Main Thread Exiting"); } }
Java Code:New thread: Thread[One,5,main] New thread: Thread[Two,5,main] Thread One is alive: true Thread Two is alive: true Waiting for Threads to finish Output 1 Output 2 Output 1 Output 2 Thread One is alive: false Thread Two is alive: false Main Thread Exiting
- 11-07-2011, 02:57 PM #4
Re: How to run two methords at the same time
Well you're going to need two instances of Runnable, or two subclasses of Thread then. Recommended reading: Defining and Starting a Thread (The Java™ Tutorials > Essential Classes > Concurrency)
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-07-2011, 03:36 PM #5
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
Re: How to run two methords at the same time
I've changed the code. Is it possible to syncroizre these methords by using wait() and notifyAll()? Would there be any performance issue here for larger methords, if so what improvments could be made?
OUTPUTJava Code:package threads; import java.lang.Thread; import java.lang.Runnable; import java.lang.InterruptedException; /** * * @author steven.haynes */ public class Threads { private static class Threads1 implements Runnable { Threads1() { } public void Output1() { System.out.println("Output 1"); } public void run() { Output1(); } } private static class Threads2 implements Runnable { Threads2() { } public void Output2() { System.out.println("Output 2"); } public void run() { Output2(); } } public static void main(String[] args) { Thread ob1 = new Thread(new Threads1()); Thread ob2 = new Thread(new Threads2()); ob1.start(); ob2.start(); System.out.println("Thread " + ob1.getName() + " is alive: " + ob1.isAlive()); System.out.println("Thread " + ob2.getName() + " is alive: " + ob2.isAlive()); try { System.out.println("Waiting for Threads to finish"); ob1.join(); ob2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + ob1.getName() + " is alive: " + ob1.isAlive()); System.out.println("Thread " + ob2.getName() + " is alive: " + ob2.isAlive()); System.out.println("Main Thread Exiting"); } }
Thread Thread-0 is alive: true
Thread Thread-1 is alive: true
Waiting for Threads to finish
Output 1
Output 2
Thread Thread-0 is alive: false
Thread Thread-1 is alive: false
Main Thread Exiting
- 11-07-2011, 03:39 PM #6
Re: How to run two methords at the same time
Why do you want to synchronize them? If you're synchronizing them, why use threads at all? What exactly do you want to happen?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-07-2011, 03:41 PM #7
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
Re: How to run two methords at the same time
No reason just experimenting.
- 11-07-2011, 03:44 PM #8
Re: How to run two methords at the same time
Okay, well, what you're saying doesn't make a ton of sense to me. Basically, calling two methods from two different threads causes them to happen at the same time (or appear to, at least). Synchronizing the methods prevents them from happening at the same time. You're basically cancelling out any benefit that threading gives you.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-07-2011, 03:47 PM #9
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
Re: How to run two methords at the same time
If i wanted to have control over my threads i would synchronize them?
- 11-07-2011, 03:54 PM #10
Re: How to run two methords at the same time
I don't know, maybe, it depends, and your example doesn't give enough information to give a straight answer. There are quite a few things you can do to have control over your threads.
Recommended reading: 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!
Similar Threads
-
making a Time class but Universal time always says 0
By Bravid2001 in forum New To JavaReplies: 2Last Post: 10-24-2011, 07:55 AM -
Time lost during time measuring
By Kobe in forum Advanced JavaReplies: 6Last Post: 08-29-2011, 03:18 PM -
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
calculate time diff for particular time period
By baktha.thalapathy in forum New To JavaReplies: 2Last Post: 05-24-2010, 04:10 PM -
Class Time - represents time of day
By verbazon in forum New To JavaReplies: 1Last Post: 04-13-2009, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks