1, Synchronized guarantee synchronization
Let's look at a class that generates even numbers.
[Java]View Plaincopy
- <span style="FONT-SIZE:16PX;" > Packagedemo.thread;
- /**
- * This is an abstract class for an int generator
- *
- */
- Public Abstract class Intgenerator {
- Private volatile Boolean canceled = false;
- public Abstract int next ();
- public void Cancel () {
- Canceled = true;
- }
- Public Boolean isCanceled () {
- return canceled;
- }
- }
- </span>
[Java]View Plaincopy
- <span style="FONT-SIZE:16PX;" >/*
- * Generate even numbers
- */
- Class Evengenerator extends Intgenerator {
- private int currentevenvalue = 0;
- String s = "";
- @Override
- public int Next () {
- <span style="color: #ff0000;" >synchronized </span> (s) {
- ++currentevenvalue;
- ++currentevenvalue;
- return currentevenvalue;
- }
- }
- This can also
- Public <span style= "color: #ff0000;" >synchronized </span>int Next () {
- ++currentevenvalue;
- ++currentevenvalue;
- return currentevenvalue;
- // }
- }</span>
Note that in the generation of even is to add a synchronous lock, or maybe thread 1 just executed a sentence of ++currentevenvalue, the operation, the thread 2 was robbed of the CPU, the thread 2 executes return currentevenvalue, then returned is an odd number. Plus synchronized is the code of two threads that can execute synchronized blocks at the same time only one thread .
Test code:
[Java]View Plaincopy
- <span style="FONT-SIZE:16PX;" > Packagedemo.thread;
- Import Java.util.concurrent.ExecutorService;
- Import java.util.concurrent.Executors;
- /*
- * Consumption figures
- */
- Public class Evenchecker implements Runnable {
- private Intgenerator Generator;
- private final int id;
- Public Evenchecker (intgenerator g, int ident) {
- Generator = g;
- id = ident;
- }
- public Void Run () {
- While (!generator.iscanceled ()) {
- int val = Generator.next ();
- if (val% 2 = 0) {//If not even
- System.out.println (val + "not enen!");
- Generator.cancel ();
- }
- }
- }
- public static void Test (Intgenerator GP, int count) {
- Executorservice exec = Executors.newcachedthreadpool ();
- For (int i = 0; i < count; i++)
- Exec.execute (New Evenchecker (GP, i));
- Exec.shutdown ();
- }
- public static void Test (Intgenerator gp) {
- Test (GP, 10);
- }
- public static void Main (string[] args) {
- Test (New Evengenerator ());
- }
- }</span>
Analysis: If the class that produces even numbers is not synchronized, then the test program will appear odd to cause the program to exit.
2, volatile indicates atomicity, visibility.
For variables shared between multiple threads, each thread has its own copy, and when thread 1 changes the value of the variable, other threads do not immediately know that the variable value has changed, and volatile guarantees that the value of the variable is visible to each thread, and that the value is changed by a thread in the other thread immediately. Atomicity indicates that the operation is not interruptible, such as the basic variable assignment.
code example:
[Java]View Plaincopy
- <span style="FONT-SIZE:16PX;" > Packagedemo.thread;
- Public class Volatiledemo implements Runnable {
- Private volatile int i = 0; Volatile setting visibility
- public synchronized int GetValue () {
- return i;
- }
- private synchronized void Enenincrement () {
- i++;
- i++;
- }
- @Override
- public Void Run () {
- While (true)
- Enenincrement ();
- }
- public static void Main (string[] args) {
- Volatiledemo at = new Volatiledemo ();
- new Thread (at). Start ();
- While (true) {
- int val = At.getvalue ();
- if (val% 2 = 0) {//appears odd, exits the program
- System.out.println (val+"is not enen!");
- System.exit (0);
- }
- }
- }
- }
- </span>
Note that the i++ operation is not an atomic line operation, and the GetValue () method should also be added synchronized.
Java Multithreading Summary Four: volatile, synchronized example