File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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...");




I agree. Here's the link:
subject: Running some part of code as a Thread
It's not a secret anymore!