-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaThread3.java
More file actions
33 lines (30 loc) · 990 Bytes
/
JavaThread3.java
File metadata and controls
33 lines (30 loc) · 990 Bytes
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oops2;
/**
*
* @author Sunil Shetty
*/
/*
Concurrency Problems
Because 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.
*/
//A code example where the value of the variable amount is unpredictable
public class JavaThread3 extends Thread {
public static int amount = 0;
public static void main(String[] args) {
JavaThread3 thread = new JavaThread3();
thread.start();
System.out.println(amount);
amount++;
System.out.println(amount);
}
public void run() {
amount++;
}
}