This experiment mainly investigates the multi-threaded operation of the singleton mode, and the multi-threaded reading of the same resource, two knowledge. The experiment involves three classes:
1) A Pojo class student, including the Set/get method.
2) A thread class that sets the member variable for student the value of age and name to 111 and 111
3) Another thread class that sets the member variable for student the value of age and name to 222 and 2222
4) Main class, For Loop 200 times, create 200 threads 1 and thread 2 access to the same resource respectively. (A total of 400 threads)
1. The first case: The A Hungry Man single mode ensures that the same object is manipulated by multi-threading
//A Hungry man single-case mode Pojo class
Public classStudent {PrivateString age =" A"; PrivateString name ="Tome"; Private StaticStudent Student =NewStudent ();//Create objects when class loads PublicString getnameandage () {returnname+":"+Age ; } Public voidSetnameandage (String name,string age) { This. Name =name; This. Age =Age ; } PrivateStudent ()//privatization of constructor functions { } Public StaticStudent Getinstace () {//method area functions, static functions returnstudent; }}
Thread 2 class:
Public class MyThread extends Thread { @Override publicvoid run () { TODO auto-generated method stub System. out. println (Student.getinstace (). Hashcode ());} }
To test a class, create and start 400 threads:
Public classAppmain implements runnable{ Public Static voidMain (string[] args) {Appmain Appmain=NewAppmain (); for(inti =0;i< $; i++) {Thread thread1=NewThread (appmain);//threads 1 MyThread thread2=NewMyThread ();//thread 2 thread1.start (); Thread2.start (); }} @Override Public voidrun () {//TODO auto-generated Method StubSystem. out. println (Student.getinstace (). Hashcode ()); }}
Results:
2. The second scenario: The Write method of the shared resource does not set any synchronization, and multiple threads can cross-write the data
Public String getnameandage () { return name+":"+ age; } Public void Setnameandage (String name,string Age) {//No write synchronization is set for this. Name = name ; this. Age = Age ; }
Two thread manipulation classes:
Public class MyThread extends Thread { @Override publicvoid run () { // TODO auto-generated Method stub Student.getinstace (). Setnameandage ("111""111" );//Set the name and age value to 1
System.out.println (Student.getinstace (). Getnameandage ();); }}
Thread 2
Public class Appmain implements runnable{
Public Static voidMain (string[] args) {Appmain Appmain=NewAppmain (); for(inti =0;i< $; i++) {Thread thread1=NewThread (Appmain); MyThread thread2=NewMyThread (); Thread1.start (); Thread2.start (); }} @Override Public voidrun () {//TODO auto-generated Method StubStudent.getinstace (). Setnameandage ("222","2222"); //set name and age to 2
System.out.println (Student.getinstace (). Getnameandage ();); }}
Execution Result:
3. The third case: The Write method of the shared resource sets the synchronization synchronized to ensure that only one thread can execute the write at the same time and then release the lock after execution.
Public String getnameandage () { return name+":"+ age; } Public void Setnameandage (String name,string Age) {//write method settings synchronized this . Name = name; this. Age = Age ; }
Test class to add printing:
public static void main (string[] args) {appmain Appmain = new Appmain (); for (int i =0 ; I<200 ; I++) {Thread Thread1 = new Thread (Appmain); MyThread thread2 = MyThread (); Thread1.start (); Thread2.start (); System. out .println (Student.getinstace (). Getnameandage ());//Add print, display name and age value}}
This allows multiple threads to set the name and set values sequentially. But why do test results still have dirty data? Like 111:222 of this dirty data?
Answer: Because the Singleton object is not set to read the lock of the Get method, so the Read method can get the value at any time, even if the set thread has not finished executing, because there is no synchronized limit can be accessed at any time.
4. In the fourth case, the Read method of the shared resource is not synchronized, so it is convenient to read the lock-free limit at any time. But as I said before, it will read the data when the thread has not finished executing, causing data confusion. Because the read thread can be read at any time, there is no lock limit.
Public //Read method does not do synchronous synchronized processing, can be read at any time, you can read out the write thread did not finish the intermediate data return name+":"+ age; } Public void Setnameandage (String name,string age) { this. Name = name; this. Age = Age ; }
Operation Result:
5. The fifth case, the reading method is also set synchronized, the lock object is also this. Make sure you don't read when you write, and you can't write when you read it. That is, read and write with the same lock.
Public String getnameandage () { return name+":"+ age; } Public void Setnameandage (String name,string age) { this. Name = name; this. Age = Age ; }
Test results:
This makes all the data accurate, but it's inefficient because read-write sets a lock together. You can't write when you read, you can't read when you write. All are accessed by order.
Conclusion: when the multi-threaded common access to the same resource, this shared object read and write methods, to set the same lock, to ensure that the write can not read, read the time can not write, and read and write are executed sequentially. To ensure the accuracy of the data.
At the same time, it also shows that there is no way to set the lock can be executed at any time, at any time, can be CPU scheduled to interrupt the execution of the thread, and read to the thread to execute half of the resulting dirty data.
Java Multithreading Knowledge points summary (b)---Multi-threaded instance parsing