Synchronization Thread in java

Friday, January 8th, 2016 (0 Comment)

In previous articles, we know the basics of how to create a thread, in the next section of this thread, we’ll learn more about synchronization Thread.

Situation: There are two Thead (t1, t2) and retrieve one object data is m variable. t1 want to read the m variable and t2 want to record m variable. –> t1 read data but can be inconsistent.

  • If t2 write data into m variable before t1 read data then t1 read data consistent at that time.
  • There should be mechanisms to allow only one thread can access common data (shared data) at one point.

This technique is called “SYNCHRONIZATION THREAD

1. The basic technique of synchronization Thread

Create one object manage the synchronization thread of a data manipulation of the Thread by executing a task status of the Thread with each time only one Thread with the synchronized keyword.

Create an object (MONITOR) to be monitoring the data objects –> called is Thread MONITOR.

Example for sync Threads by MONITOR:

The program will export 3 No. 10.11, 12 the screen, each number is one thread execution.

Synchronized keyword with the management report synchronization Thread.

Main class

  • Line 6,7,8: Three Thread have different value is 10.11, 12 but which share one monitor.

Output:

2. Technique synchronized blocks

Synchronize a task block.

The programmer can not want to use the synchronized method to synchronize access to object.

The classes provided by the library or by someone offers – the class created so should not be adding “synchronized” keyword to these methods.

Syntax sync blocks

Example for synchronized block: This program rewritten from the previous example, ignore synchronized keyword in Monitor1 class.

In OutNum class, modify run method by syntax synchronized block

3. Deadlock

Deadlock: situations not exit, freezing, occurs when the Threads waiting resources (monitor) of each other’s resource and form a cycle. Deadlock occurs rarely.

Analysis following program:

  • An application include 2 Thread : Thread t1 in Object o1, Thread t2 in Object o2
  • Monitor of t1 is o2 and monitor of t2 is o1 (cross).
  • Both Thread1 and Thread1 call behavior synchronized run () and with 200 milliseconds sleep. As share CPU time, t1 sleep before t2 (detail at method run ()).
  • When t1 wake up, monitor’s Synchro() method of o2 (include t2) called but include t2 sleeping, this method cannot enforcement.
  • When t2 wake up, monitor’s Synchro() method of o1 (include t1) called but include t2 sleeping, this method cannot enforcement.
  • So the program will freeze (blocked) not do anything.

4. Mechanism waiting, identification

Java provides a mechanism in inter-process communication to the Threads can call each other (ask each other) using final the methods of the Object: wait (), notify (), notifyAll () . So every class can use them and this method can only be called in the synchronized methods.

Tags: , ,

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Leave a Reply

Your email address will not be published. Required fields are marked *