Reputation: 25
I have to create an application in java using threads, but I am new to multithread programing in java. The applicatoin will have many tasks to do, where each task shold be run by a thread. Then I will implement Round Robin algorithm also in java to schedule threads works.
I am not sure if it works properly but this is the code I have done until now (the application with 8 tasks). If it is Ok now I should implement the RR scheduling algorithm, how can I do it? Should it be implemented in a new file or in within main method?
public class Application extends Thread {
public class Task1 extends Thread {
@Override
public void run() {
//task 1
}
}
public class Task2 extends Thread {
@Override
public void run() {
//task2
}
}
public class Task3 extends Thread {
public void run(){
//task3
}
}
public class Task4 extends Thread {
public void run(){
//task4
}
}
public class Task5 extends Thread {
public void run(){
//task5
}
}
public class Task6 extends Thread {
public void run(){
//task6
}
}
public class Task7 extends Thread {
public void run(){
//task7
}
}
public class Task8 extends Thread {
public void run(){
//task8
}
}
public static void main(String[] args) {
Application a=new Application();
Task1 t1=a.new Task1();
//etc
}
}
Upvotes: 1
Views: 1983
Reputation: 272427
Do you have to implement your own scheduling ? Can you instead use an Executor and let it look after the scheduling of your jobs? From the Javadoc:
An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
Upvotes: 3