Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
52 lines (44 loc) · 1.42 KB

File metadata and controls

52 lines (44 loc) · 1.42 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package VolatileKeyword_2;
/**
* Volatile Keyword, <em>“… the volatile modifier guarantees that any thread that
* reads a field will see the most recently written value.”</em> - Josh Bloch
* <br><br>
* Codes with minor comments are from
* <a href="http://www.caveofprogramming.com/youtube/">
* <em>http://www.caveofprogramming.com/youtube/</em>
* </a>
* <br>
* also freely available at
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
* </a>
*
* @author Z.B. Celik <celik.berkay@gmail.com>
*/
import java.util.Scanner;
class Processor extends Thread {
private volatile boolean running = true;
public void run() {
while (running) {
System.out.println("Running");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void shutdown() {
running = false;
}
}
public class App {
public static void main(String[] args) {
Processor pro = new Processor();
pro.start();
// Wait for the enter key
System.out.println("Enter something to stop the thread,\nVolatile variable running will be forced to true :");
new Scanner(System.in).nextLine();
pro.shutdown();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.