This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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

Running some part of code as a Thread

M K Rayapudi
Ranch Hand

Joined: Feb 19, 2007
Posts: 160

How can I run the method (doSomeProcess) as a thread?
please help

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( DoSomeProcess()).start();
new Thread( 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
It's not a secret anymore!