aspose file tools*
The moose likes Threads and Synchronization and the fly likes Running some part of code as a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Running some part of code as a Thread" Watch "Running some part of code as a Thread" New topic
Author

Running some part of code as a Thread

M K Rayapudi
Ranch Hand

Joined: Feb 19, 2007
Posts: 157
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...");

}


}

}

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running some part of code as a Thread