Ynchronized role
Synchronized role
A synchronization method
Public synchronized void methodAAA () (
/ /….
)
Calling the lock is the object of this synchronization method
Test:
A, do not use this keyword modified method, called two threads with an object of this method.
Goals categories:
1public class TestThread public void execute () for (int i = 0; i <100; i + +) System.out.println (i);
5)
6)
7)
Thread categories:
1public class ThreadA implements Runnable TestThread test = null;
3 public ThreadA (TestThread pTest) = pTest test;
5)
6 public void run () test.execute ();
8)
9)
Call:
1TestThread test = new TestThread ();
2Runnable runabble new ThreadA = (test);
3Thread a = new Thread (runabble "A");
4a.start ();
5Thread b = new Thread (runabble, "B");
6b.start ();
Results:
Output figures intertwined. That is not synchronized, the two methods in different thread is the asynchronous call.
B, revised goal of increased synchronized modified
1public class TestThread public synchronized void execute () for (int i = 0; i <100; i + +) System.out.println (i);
5)
6)
7)
Results:
Output figures are orderly, first of all, the number of output A, then B, that is synchronous, although different threads, but the method is called synchronization.
Note: Although the above are two different threads, but it is an example of the same object. Below the use of different examples of targets for testing.
C, each thread has its own TestThread object.
Goals categories:
1public class TestThread public synchronized void execute () for (int i = 0; i <100; i + +) System.out.println (i);
5)
6)
7)
Thread categories:
1public class ThreadA implements Runnable public void run () = new TestThread TestThread test ();
4 test.execute ();
5)
6)
7
Call:
1Runnable runabble new ThreadA = ();
2Thread a = new Thread (runabble "A");
3a.start ();
4Thread b = new Thread (runabble, "B");
5b.start ();
Results:
Output figures intertwined. Although the increase in the synchronized keyword modification methods, but different threads call their own object instance, the two methods is asynchronous.
Extension:
For many such examples, in order to synchronize the digital output that is orderly and output by threading sequence, we can add a static variable, it conducted locking (Note will be locked behind the object).
Laws target categories:
1public class TestThread private static Object lock = new Object (); / / must be static.
3 public void execute () synchronized (lock) for (int i = 0; i <100; i + +) System.out.println (i);
7)
8)
9)
10)
Second, synchronous code block
1public void method (SomeObject so) synchronized (so)
3 / /… ..
4)
5)
Lock an object, in fact lock is the object references (object reference)
Who can get the locks will have control of that part of its operation code. When there is a clear target as a lock, they can write code according to the above procedures, but there is no clear target as a lock, a section of code just want to sync, you can create a special instance variables (it must be an object) to act as Lock (above solution is to increase a state of lock).
A, lock an object, it is not static
Private byte [] = new byte lock [0]; / / special instance variables target categories:
1public class TestThread private Object lock = new Object ();
3 public void execute () synchronized (lock)
6 different because there are different examples of object-locking lock
7 for (int i = 0; i <100; i + +) System.out.println (i);
9)
10)
11)
12)
In fact, a lock above, equivalent to the following:
1public void execute () synchronized (this) for (int i = 0; i <100; i + +) System.out.println (i);
5)
6)
7)
B, or lock an object, it is such a lock static, it is the object lock in the category
Public synchronized static void execute () (
//…
)
Equivalent to
1public class TestThread public static void execute () synchronized (TestThread.class) / /)
6)
7)
Test:
Goals categories:
1public class TestThread private static Object lock = new Object ();
3 public synchronized static void execute () for (int i = 0; i <100; i + +) System.out.println (i);
6)
7)
8 public static void execute1 () for (int i = 0; i <100; i + +) System.out.println (i);
11)
12)
13 public void test () and execute () / / output is orderly, that is synchronized
15 / / execute1 () / / output is not required, that is asynchronous
16)
17)
Thread categories: call different methods, so the establishment of the two threads
1public class ThreadA implements Runnable public void run () synchronous TestThread.execute ();// call static methods
4)
5)
6public class ThreadB implements Runnable public void run () = new TestThread TestThread test ();
9 test.test ();// call non-synchronous non-static method
10)
11)
Call:
1Runnable runabbleA new ThreadA = ();
2Thread a = new Thread (runabbleA "A");
3a.start ();
4Runnable runabbleB = new ThreadB ();
5Thread b = new Thread (runabbleB, "B");
6b.start ();
NOTE:
1, to lock in a synchronized with the object, if the object in the lock code has been modified in the paragraph, then lock it disappeared. Look at the following examples:
Goals categories:
1public class TestThread private static final class TestThreadHolder private static TestThread theSingleton = new TestThread ();
4 public static TestThread getSingleton () return theSingleton;
6)
7 private TestThreadHolder ())
9)
10
11 private Vector ve = null;
12 private Object lock = new Object ();
13 private TestThread () ve = new Vector ();
15 initialize ();
16)
17 public static TestThread getInstance () return TestThreadHolder.getSingleton ();
19)
20 private void initialize () for (int i = 0; i <100; i + +) ve.add (String.valueOf (i));
23)
24)
25 public void reload () synchronized (lock) ve = null;
28 ve = new Vector ();
29 / / lock = "abc";
30 for (int i = 0; i <100; i + +) ve.add (String.valueOf (i));
32)
33)
34 System.out.println ( "reload end");
35)
36
37 public boolean checkValid (String str) synchronized (lock) System.out.println (ve.size ());
40 return ve.contains (str);
41)
42)
43)
Description: In reload and checkValid method increases the synchronized keyword, the lock object locking. In different threads in the same types of objects called reload and checkValid methods.
In reload method, the object is not to modify lock Notes lock = "abc"; As a result, the console output after the output reload end 100. This is called synchronization.
If reload method modified lock object removed Notes, the first results of a digital output (the size of the current ve), and then reload end output. That is asynchronous transfer.
2, single-mode in case of multi-threaded consideration
1public class TestThread private static final class TestThreadHolder private static TestThread theSingleton = new TestThread ();
4 public static TestThread getSingleton () return theSingleton;
6)
7 private TestThreadHolder ())
9)
10 private Vector ve = null;
11 private Object lock = new Object ();
12 private TestThread () ve = new Vector ();
14 initialize ();
15)
16 public static TestThread getInstance () return TestThreadHolder.getSingleton ();
18)
19'' '
20)
Description: An internal category increase in the internal category affirms that a static objects, instances of the single case of categories, the initialized data in a single case of the constructor function in a statement. This guarantee also visit a number of examples when the initialization of data has been successfully initialized.
Summary:
A. Whether increase in the synchronized keyword method or target, it has a lock are objects, rather than a section of code or function as a lock, it should first be aware of the need to lock the target
B. There is only one of each object lock (lock) associated with it.
C. Synchronized to the system overhead as a great price, and may even cause deadlock, as far as possible to avoid unnecessary synchronization control.
Published in 2007-05-08 11:02 swingboat reading (928) Comments (0) edit their collections quoted Category: JAVA, design patterns






