-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundThread.java
52 lines (47 loc) · 1.23 KB
/
BackgroundThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* BackgroundThread class
* Allows for asynchronous execution of a code when a
* background app is passed to it.
* when .start() is invoked, it will start a separate thread
* of execution.
*/
public class BackgroundThread implements Runnable {
private Thread backgroundThread;
private App app;
/*
* Constructor for a background thread object, pass the background
* app that will be started asynchronously.
*/
public BackgroundThread(App app) {
this.app = app;
backgroundThread = new Thread(this);
}
/*
* Starts the background application. the execution will be in its own
* thread.
*/
public void start() {
backgroundThread.start();
}
/*
* Allows the application to execute asynchronously, when
* app.start() method is invoked. Will invoke backgroundStart()
* method.
*/
public void run() {
try {
while(!backgroundThread.isInterrupted()) {
app.backgroundStart();
}
Thread.sleep(100); //Can be interrupted
}
catch(InterruptedException e) {}
app.exit();
}
/*
* Will exit the application and stop executing the program.
*/
public void exit() {
backgroundThread.interrupt();
}
}