Synchronous processing in Java

xiaoxiao2021-03-06  40

The synchronization here only for a single Java application, as for the database synchronization, file synchronization, etc., is not the problem discussed here.

Synchronous and multi-threaded relationship

1. There is no need to synchronize without multi-threaded environments.

2. Even if there are multithreaded environments, it is not necessarily synchronized.

Why is it synchronized:

In order to prevent multiple threads from modification of the same data, you need to synchronize, otherwise the data is inconsistent.

Java provides a very convenient multi-threaded support, so it is more common, especially servlet and

JSP thread security issues are particularly obvious.

http://blog.9cbs.net/treeroot/archive/2004/09/03/93881.aspx

Synchronous essence:

Java synchronization is actually getting a synchronous lock of an object, and other threads can only wait. Most of our programs

Not thread security, because there is no synchronization, and we don't have necessary, because most of the situation is not

There are multithreaded environments.

Only an example of the essence of synchronization.

Code 1:

Class syntest {

Synchronized static method () {

// This method is the method of synchronization, and only one thread can come in each time.

System.out.println ("Begin");

Try {threeRead.sleep (2000);

System.out.println ("end");

}

Public static void main (String [] args) {

// Start 10 threads, here is anonymous, because it is very convenient.

For (int i = 0; i <10; i ) {

New thread () {

Public void run () {

Method ();

}

} .start ();

}

}

}

It is easy to see that each thread must wait for the previous thread to exit method Method can go in.

The above synchronization method can have two equivalents:

Code 2:

Class syntest {

Static method () {

Synchronized (syntest.class) {

// This method is not a synchronous method, but only one thread can enter the synchronization block each time.

System.out.println ("Begin");

Try {threeRead.sleep (2000);

System.out.println ("end");

}

}

Public static void main (String [] args) {

// Start 10 threads, here is anonymous, because it is very convenient.

For (int i = 0; i <10; i ) {

New thread () {

Public void run () {

Method ();

}

} .start ();

}

}

}

Code 3:

Class syntest {

Private static object lock = new object ();

Static method () {

Synchronized (LOCK) {

// This method uses a dedicated object as a lock.

System.out.println ("Begin");

Try {threeRead.sleep (2000);

System.out.println ("end");

}

}

Public static void main (String [] args) {

// Start 10 threads, here is anonymous, because it is very convenient.

For (int i = 0; i <10; i ) {

New thread () {

Public void run () {

Method ();

}

} .start ();

}

}

In fact, the three situations are basically equivalent, but the third way is very common, and it is estimated that there will be performance.

Gao, after all, locked a small object seems to be more likely to be a big object than locking a big object.

The above is a static method. If it is non-static, the performance of code 2 is Synchronized (this).

Here, you will not explain that a point, synchronous and whether thread safety and method are static.

If you judge whether a class or a method is secure?

Suppose a class does not provide a direct modification of the member variable, that is, it can only be modified by way

Object. We can easily draw conclusions, only all methods are thread safety, this class is thread

safe. So how to determine if a method is secure, only to see the document or read the source code.

However, it is certain that most of them are not thread safe, from the method statement that can not be seen.

Class Math {

Public Static int SafePow (int X, int y) {// hypothesis y is greater than 0

INT RES = X;

For (int i = 1; i

Return res;

}

Public Static Int UnsafePow (int X, int y) {

Pow = Y;

Return Pow (x);

}

Private statin

Private statin {INT X) {

INT RES = X;

For (int i = 1; i

Return res;

}

}

I don't know how to return to such a boring example, and I found that this problem is very clear, it is difficult

Express it clearly, look at the two threads call the unsafepow example.

Thread1: unsafepow (3, 3);

Thread2: unsafepow (2, 2);

Suppose Thread1 is executed to the POW = Y; enter the method Pow (INT X);

At this time, POW = 3;

Then thread2 performs POW = Y; enter the method Pow (int X);

At this time, POW = 2;

The thread 1 performs a POW (INT X) method should wait for 3, resulting in errors, so say

This method is not a thread safe.

The key to thread safety is to modify the member variable (class variable or instance variable, both here)

Same), and there is a thread security problem in the access method.

We know that haShtable is a thread safe, because all methods are synchronized, that is, the most can only be

There is a thread to HashTable operation, then we think it is a thread. So, suppose its size () method is not synchronized,

Then there may be that you call the size method has not returned, and a thread has added a data. At this time, the value you read should be correct.

The value, but this size method has not returned, and another thread deletes a data. At this time, you return it to an error size.

HashTable also needs to synchronize, of course, hashtable is a thread safe, and the Java document has a description, but not

Don't synchronize synchronous pressure equal to thread safety.

Public class synhatable {

Public static void main (string [] args)

{

Final hashtable ht = new hashtable ();

HT.PUT (New Object (), "Quick to Death");

New thread () {

Public void run () {

While (true) {

IF (ht.size () <10) {ht.put (New

Object (), "object");

}

Else {

ht.clear ();

}

}

}

} .start ();

New thread () {

Public void run () {

UNSAFE (HT);

}

} .start ();

}

Static void unsafe (havehtable HT) {

While (true) {

// synchronized (ht) {

Iterator it = ht.values ​​(). Iterator ();

While (it.hasnext ())

System.out.println (it.next ());

}

//}

}

}

If you don't sync, you will crash soon.

转载请注明原文地址:https://www.9cbs.com/read-60747.html

New Post(0)