This is the first time I am writing multiple threaded program. I have a doubt that multiple thread which I'l create will point to the same run method and perform the task written in run(). but I want different threads to perform different tasks e.g 1 thread will insert into database other update and etc. My question is how to create different threads that will perform different tasks
Asked
Active
Viewed 5,865 times
4
-
implement `java.lang.Runnable` – xiaofeng.li Jun 06 '12 at 06:37
5 Answers
4
Create your threads that do different jobs:
public class Job1Thread extends Thread {
@Override
public void run() {
// Do job 1 here
}
}
public class Job2Thread extends Thread {
@Override
public void run() {
// Do job 2 here
}
}
Start your threads and they will work for you:
Job1Thread job1 = new Job1Thread();
Job2Thread job2 = new Job2Thread();
job1.start();
job2.start();

alexey28
- 5,170
- 1
- 20
- 25
3
you can create different classes implementing Runnable with different jobs - just for start

Abhishek Choudhary
- 8,255
- 19
- 69
- 128
3
You can run the run() method with your conditions (insert database, update, etc). While initializing your thread class, pass argument in class constructor, which will define what task this thread will do for you.

Ankit
- 3,083
- 7
- 35
- 59
2
/* This program creates three different threads to perform three different tasks. Thread -1 prints A...Z, Thread-2 prints 1...100, Thread-3 prints 100-200. Very basic program for understanding multi-threading without creating different classes for different tasks */
class ThreadingClass implements Runnable {
private Thread t;
private String threadName;
ThreadingClass( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
if( threadName == "Thread-1" ){
this.printAlpha();
}
else if( threadName == "Thread-2" ){
this.printOneToHundred();
}
else{
this.printHundredMore();
}
}
public void printAlpha(){
for(int i=65; i<=90; i++)
{
char x = (char)i;
System.out.println("RunnableDemo: " + threadName + ", " + x);
}
}
public void printOneToHundred(){
for(int i=1; i<=100; i++)
{
System.out.println("RunnableDemo: " + threadName + ", " + i);
}
}
public void printHundredMore(){
for(int i=100; i<=200; i++)
{
System.out.println("RunnableDemo: " + threadName + ", " + i);
}
}
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this);
t.start ();
t.setName(threadName);
}
}
}
public class MultiTasking {
public static void main(String args[]) {
ThreadingClass R1 = new ThreadingClass ( "Thread-1");
ThreadingClass R2 = new ThreadingClass ( "Thread-2");
ThreadingClass R3 = new ThreadingClass ( "Thread-3");
R1.start();
R2.start();
R3.start();
}
}

Aditya Chauhan
- 104
- 1
- 6
1
You can use inner class for this. Like below
class first implements Runnable
{
public void run(){
System.out.println("hello by tobj");
}
public static void main(String args[]){
first obj=new first();
Thread tobj =new Thread(obj);
tobj.start();
Thread t2 =new Thread(obj)
{
public void run()
{
System.out.println("hello by t2");
}
};
Thread t = new Thread(obj)
{
public void run()
{
System.out.println("hello by t");
}
};
t.start();
t2.start();
}
}

Aditya Jagtap
- 76
- 8
-
using object of a class is not required for inner class.But it won't stop you doing that. – Aditya Jagtap Feb 07 '16 at 06:51