Multithreading in JAVA Study Notes (I): thread synchronization Security Processing
I believe everyone is familiar with multi-threaded operations. I don't need to describe in detail how to enable a thread or the like. Today we will talk about thread synchronization security.
Thread Synchronization security issues are generally caused by concurrent operations on the same resource by multiple threads. In this case, we need to synchronize and protect public resources. There are three situations at this time.
1. synchronous code block. The synchronized lock is any object;
2. For method synchronization, the lock for synchronization is the class of the method;
3. Static Method synchronization. The lock for this synchronization is the bytecode of the class where the method is located.
Next, we will give an example to illustrate how to achieve resource synchronization security when multiple threads operate on the same resource. We use the example of production and consumption to produce a commodity and consume a commodity cycle. It also describes the usage of thread wait and wake-up in thread synchronization.
See the Code:
Public class ThreadTest {public static void main (String [] args) {// resource Resources r = new Resources (); // production factory Producer pro = new Producer (r ); // Consumer con = new Consumer (r); // four threads simultaneously produce and consume, one for production and one for consumption, use Thread t1 = new Thread (pro); Thread t2 = new Thread (pro); Thread t3 = new Thread (con) to avoid repeated and chaotic resources ); thread t4 = new Thread (con); t1.start (); t2.start (); t3.start (); t4.start ();} public static class Resourc Es {public Resources () {}; // product name private String name; // product no. private int count = 1; // loop flag private boolean flag = false; // production method public synchronized void produce (String name) {// loop flag while (flag) {try {this. wait () ;}catch (Exception e) {}// produce a product this. name = name + "--" + count ++; System. out. println (Thread. currentThread (). getName () + "make a commodity" + this. name); // tag bit conversion, entering the loop flag = true; // The first line in the thread queue to avoid Wakeup Thread is not a consumption thread. We choose to wake up this. notifyAll () ;}// consumption method public synchronized void consume () {while (! Flag) {try {this. wait () ;}catch (Exception e) {}} System. out. println (Thread. currentThread (). getName () + "consume a commodity" + this. name); // mark bit conversion, entering the loop flag = false; // to avoid waking up the first thread in the thread queue is not the production thread, we choose to wake up all this. yyall () ;}// Producer static class Producer implements Runnable {private Resources res; Producer (Resources res) {this. res = res;} public void run () {while (true) {res. produce ("item") ;}}// Consumer static class Consumer implements Runnable {private Resources res; Consumer (Resources res) {this. res = res;} public void run () {while (true) {res. consume ();}}}}
We found that in the above process, we will wake up all the waiting threads, and then control production and consumption according to the loop mark. This always gives a very bad feeling. When there are many threads, thread waste will occur. After JDK5.0, the thread synchronization Lock class is provided to solve this problem. The Lock class can replace the synchronization keyword to protect public resources. I can see the Code directly:
Import java. util. concurrent. locks. condition; import java. util. concurrent. locks. lock; import java. util. concurrent. locks. reentrantLock; public class ThreadTest2 {public static void main (String [] args) {Resource r = new Resource (); Producer pro = new Producer (r ); consumer con = new Consumer (r); Thread t1 = new Thread (pro); Thread t2 = new Thread (pro); Thread t3 = new Thread (con ); thread t4 = new Thread (con); t1. Start (); t2.start (); t3.start (); t4.start ();} static class Resource {// create a Lock private lock Lock = new ReentrantLock (); private String name; private int count = 1; private boolean flag = false; // obtain the corresponding wake-up Condition private Condition proCondition = lock. newCondition (); private Condition conCondition = lock. newCondition (); public void product (String name) {// lock. lock (); try {while (flag) {// wait for proCondition. await ();} This. name = name + "--" + count ++; System. out. println (Thread. currentThread (). getName () + "producer" + this. name); flag = true; // wake up the consumption thread conCondition. signal ();} catch (Exception e) {} finally {// unlock lock. unlock () ;}} public void consume () {// lock. lock (); try {while (! Flag) {conCondition. wait ();} System. out. println (Thread. currentThread (). getName () + "Consumer ----" + this. name); flag = false; // wake up the producer proCondition. signal ();} catch (Exception e) {} finally {// unlock lock. unlock () ;}} static class Producer implements Runnable {private Resource res; Producer (Resource res) {this. res = res;} public void run () {while (true) {res. product ("item") ;}}static class Consumer implements Runnable {private Resource res; Consumer (Resource res) {this. res = res;} public void run () {while (true) {res. consume ();}}}}
For more usage of the Lock class, refer to the JDK documentation.
The above content is thread synchronization security.