Multi-threaded synchronization mechanisms lock resources so that at the same time, only one thread can operate, synchronizing the problems that may occur when multiple threads are simultaneously accessed.
The synchronization mechanism can be implemented using the SYNCHRONIZED keyword.
When the Synchronized keyword modifies a method, the method is called a synchronization method.
Locks are automatically released when the Synchronized method finishes or an exception occurs.
The following is an example to parse the usage of the Synchronized keyword.
[html] View Plain copy Example program 1 public class threadtest { public static void main (String[] args) { Example Example = new example (); thread t1 = new thread1 (example); thread t2 = new thread1 (example); t1.start (); t2.start ( ); } } class example { public synchronized void execute () { for (int i = 0; i < 10; ++i) { try { thread.sleep ( ); } catch (interruptedexception e) { e.printstacktrace (); } System.out.println ("hello: " + i); } } } class thread1 extends thread