| Author |
Running some part of code as a Thread
|
M K Rayapudi
Ranch Hand
Joined: Feb 19, 2007
Posts: 156
|
|
Hi,
How can I run the method (doSomeProcess) as a thread?
please help
|
R6i
|
 |
Nomaan Butt
Ranch Hand
Joined: Oct 19, 2011
Posts: 54
|
|
Hi Rayapudi
hope below piece of code will be usefull for you, here i have created an inner class inside Query implementing runnable interface. In the main method two thread are being created.
public class Query {
String []arr = {"sun","mon","tue","wed","thu"};
public static void main(String[] args) {
Query querObject = new Query();
new Thread(querObject.new DoSomeProcess()).start();
new Thread(querObject.new DoSomeProcess()).start();
}
class DoSomeProcess implements Runnable{
public void run(){
String res = null;
for(String input: arr ) {
if(input.equalsIgnoreCase("sun")) {
res = "Sunday";
} else if(input.equalsIgnoreCase("mon")) {
res = "Monday";
} else if(input.equalsIgnoreCase("tue")) {
res = "Tuesday";
} else if(input.equalsIgnoreCase("wed")) {
res = "Wednesday";
} else if(input.equalsIgnoreCase("thu")) {
res = "Thursday";
}
System.out.println(Thread.currentThread().getName()+": "+input+"=>"+res);
}
System.out.println("other activities...");
}
}
}
|
 |
 |
|
|
subject: Running some part of code as a Thread
|
|
|