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?
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");
}
}
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.
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.
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");
}
}
OUTPUT
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
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)
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?
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");
}
}
OUTPUT
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
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?
Re: How to run two methords at the same time
No reason just experimenting.
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.
Re: How to run two methords at the same time
If i wanted to have control over my threads i would synchronize them?
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)