View difference between Paste ID: aH8i8pGx and yCZUfpYY
SHOW: | | - or go back to the newest paste.
1
package testing;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Random;
6
7
public class Testing extends Thread{//Testing e Scheduler
8
  public static Random random = new Random();
9
  static List<Process> scheduled = new ArrayList<>();
10
11
  public static void main(String[] args) throws InterruptedException {
12
    // TODO: kreirajte 100 Process thread-ovi i registrirajte gi
13
    for(int i=0;i<100;i++)
14
    {
15
    	Process p = new Process();
16
    	register(p);
17
    }
18
    Testing scheduler = new Testing();
19
    scheduler.start();
20
    scheduler.join(20000); 
21
    if(scheduler.isAlive())
22
    {
23
    	scheduler.interrupt();
24
    	System.out.println("Terminated scheduling");
25
    }
26
    else
27
    	System.out.println("Finished scheduling");
28
    // TODO: kreirajte Scheduler i startuvajte go negovoto pozadinsko izvrsuvanje
29
    
30
    // TODO: Cekajte 20000ms za Scheduler-ot da zavrsi
31
    
32
    // TODO: ispisete go statusot od izvrsuvanjeto
33
  }
34
35
  public static void register(Process process) {
36
    scheduled.add(process);
37
  }
38
39
  public Process next() {
40
    if (!scheduled.isEmpty()) {
41
      return scheduled.remove(0);
42
    }
43
    return null;
44
  }
45
46
  public void run() {
47
    try {
48
          while (!scheduled.isEmpty()) {
49
            Thread.sleep(100);
50
            System.out.print(".");
51
            Process p = this.next();
52
            p.execute();
53
            
54
            // TODO: zemete go naredniot proces
55
            
56
            // TODO: povikajte go negoviot execute() metod
57
            
58
            // TODO: cekajte dodeka ne zavrsi negovoto pozadinsko izvrsuvanje            
59
    
60
          }
61
        } catch (InterruptedException e) {
62
          e.printStackTrace();
63
        }
64
        System.out.println("Done scheduling!");
65
      }
66
  }
67
  
68
69
70
71
class Process extends Thread{
72
73
  public Integer duration;
74
75
  public Process() throws InterruptedException {
76
    this.duration = Testing.random.nextInt(1000);
77
  }
78
79
  public void run()
80
  {
81
	  try {
82
		Thread.sleep(this.duration);
83
	} catch (InterruptedException e) {
84
		// TODO Auto-generated catch block
85
		e.printStackTrace();
86
	}
87
  }
88
89
  public void execute() {
90
    System.out.println("Executing[" + this + "]: " + duration);
91
    // TODO: startuvajte go pozadinskoto izvrsuvanje
92
    this.start();
93
    try {
94
		this.join();
95
	} catch (InterruptedException e) {
96
		// TODO Auto-generated catch block
97
		e.printStackTrace();
98
	}
99
  }
100
}