Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testing;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class Testing extends Thread{//Testing e Scheduler
- public static Random random = new Random();
- static List<Process> scheduled = new ArrayList<>();
- public static void main(String[] args) throws InterruptedException {
- // TODO: kreirajte 100 Process thread-ovi i registrirajte gi
- for(int i=0;i<100;i++)
- {
- Process p = new Process();
- register(p);
- }
- Testing scheduler = new Testing();
- scheduler.start();
- scheduler.join(20000);
- if(scheduler.isAlive())
- {
- scheduler.interrupt();
- System.out.println("Terminated scheduling");
- }
- else
- System.out.println("Finished scheduling");
- // TODO: kreirajte Scheduler i startuvajte go negovoto pozadinsko izvrsuvanje
- // TODO: Cekajte 20000ms za Scheduler-ot da zavrsi
- // TODO: ispisete go statusot od izvrsuvanjeto
- }
- public static void register(Process process) {
- scheduled.add(process);
- }
- public Process next() {
- if (!scheduled.isEmpty()) {
- return scheduled.remove(0);
- }
- return null;
- }
- public void run() {
- try {
- while (!scheduled.isEmpty()) {
- Thread.sleep(100);
- System.out.print(".");
- Process p = this.next();
- p.execute();
- // TODO: zemete go naredniot proces
- // TODO: povikajte go negoviot execute() metod
- // TODO: cekajte dodeka ne zavrsi negovoto pozadinsko izvrsuvanje
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Done scheduling!");
- }
- }
- class Process extends Thread{
- public Integer duration;
- public Process() throws InterruptedException {
- this.duration = Testing.random.nextInt(1000);
- }
- public void run()
- {
- try {
- Thread.sleep(this.duration);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void execute() {
- System.out.println("Executing[" + this + "]: " + duration);
- // TODO: startuvajte go pozadinskoto izvrsuvanje
- this.start();
- try {
- this.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment