-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx22Thread.java
More file actions
34 lines (28 loc) · 1.01 KB
/
Ex22Thread.java
File metadata and controls
34 lines (28 loc) · 1.01 KB
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
package JavaEntry;
/*
Concurrency problems: Threads run at the same time as other parts of the program, there is no way to know in which order the code will run.
When the threads and main program are reading and writing the same variables, the values are unpredictable.
The problems that result from this are called concurrency problems.
*/
/* Avoid concurrency problems, use the isAlive() method of the thread to check whether the thread has finished
*/
public class Ex22Thread extends Thread {
public static int amount = 0;
public static void main(String[] arg){
Ex22Thread customThread = new Ex22Thread();
customThread.start();
// Wait for the thread to finish
while(customThread.isAlive()) {
System.out.println("Waiting...");
}
System.out.println(amount);
amount++;
System.out.println(amount);
}
public void run(){
amount++;
System.out.println("threAD");
amount++;
amount++;
}
}