• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Running some part of code as a Thread

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How can I run the method (doSomeProcess) as a thread?
please help
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...");

}


}

}

 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic