About synchronization problems with Java multithreaded programming _ programming

Source: Internet
Author: User
Tags ticket

When it comes to threading programming, our initial understanding is that the system resources can be programmed to switch between different threads to enable the system to run efficiently. Then this seemingly straightforward problem, actually operation, but hide a lot of complex details, in which the synchronization between threads and communication problems, is to lead to multithreaded programming the key to a variety of anomalies. To give a simple example, the bank has multiple service windows, each service window through the call service. Suppose the system has designed a variable number that represents the current number, and when number is greater than 10, it is no longer in the queue. Now suppose there are three window threads 1,2,3. Thread 1 executes to ①, the value of the judge number is exactly equal to 10, ready to perform service delivery ②, the system allocated time resources to switch to the thread 2, at this point the thread 1 saved the breakpoint is the ② address information. Thread 2 runs to the judgment statement ①, found that number is still 10 to meet the conditions, into the provision of service ②, and the number plus one, when the system switched to thread 1, starting from the breakpoint ②, without judgment, executed the provision of services, and the actual number is 11, Should not be offered services. This is a thread synchronization problem, we should be through a mechanism to ensure that this phenomenon does not occur, in Java, there is a mechanism called synchronous lock synchronized to ensure that the critical region in the same time period only one thread allowed operation.

if (number<=10) ..... ①

{

to provide a service; Ii

number++ ..... ③

}

Now let's look at a Bank service window program written in a synchronized lock, an example of which I chose from a book, and I expanded it myself.

Class TICKETWINDOW3 implements Runnable
{
private static int max_value = 0;
Private Boolean flag = true;
public void Run ()
{
if (flag)
{
while (true)
{
Synchronized (Ticketwindow3.class)
{
if (Max_value > 500)
{
Break
}
Try
{
Thread.Sleep (10);
}catch (interruptedexception e)
{

}
System.out.println (Thread.CurrentThread (). GetName () + ": Lock ..." +max_value++);
}
}
}
Else
{
while (true)
if (Ticket ())
Break
}
}
Private synchronized Static Boolean ticket ()
{
if (Max_value > 500)
{
return true;
}
Try
{
Thread.Sleep (10);
}catch (interruptedexception e)
{

}
System.out.println (Thread.CurrentThread (). GetName () + ": Method ..." +max_value++);
return false;
}
public void Change () throws Interruptedexception
{
Thread.Sleep (30);
This.flag = false;
}
}
public class Bank3 {
public static void Main (string[] args) throws Interruptedexception {
TICKETWINDOW3 TW3 = new TicketWindow3 ();
thread T1 = new Thread (TW3);
Thread t2 = new Thread (TW3);
T1.start ();
Tw3.change ();
T2.start ();
}
}

In this example, the critical area is locked and the TICKETWINDOW3 class is locked in two ways, in which the two locking areas are the same and can be implemented where a thread occupies a critical area and no other thread can access it. The first way to add locks is through the synchronized lock class, which is called the This lock, and the second way is to lock a class method through synchronized ticket (), look at this program, we can not necessarily determine the second way to lock the class method is also this lock, Because there's another hypothetical explanation, the lock class method only locks one part of the class, and the first lock is larger in scope, both of which are contained relationships, although the final result does implement a thread synchronization, guaranteeing the correctness of the final result, but does not explain the problem. So I made a modification to the program so that the class had two classes of locks, Ticket1 () and Ticket2 (), without this lock.

Class TICKETWINDOW3 implements Runnable
{
private static int max_value = 0;
Private Boolean Flag1 = true;
Private Boolean Flag2 = true;
public void Run ()
{
if (FLAG1)
{
while (true)
{
Synchronized (Ticketwindow3.class)
{
if (Max_value > 500)
{
Break
}
Try
{
Thread.Sleep (10);
}catch (interruptedexception e)
{

}
System.out.println (Thread.CurrentThread (). GetName () + ": Lock ..." +max_value++);
}
}
}
Else
{
if (FLAG2)
{
while (true)
{
if (Ticket1 ())
Break
}
}
Else
{
while (true)
{
if (Ticket2 ())
Break
}
}
}
}
Private synchronized Static Boolean Ticket1 ()
{
if (Max_value > 500)
{
return true;
}
Try
{
Thread.Sleep (10);
}catch (interruptedexception e)
{

}
System.out.println (Thread.CurrentThread (). GetName () + ": Method ..." +max_value++);
return false;
}
Private synchronized Static Boolean Ticket2 ()
{
if (Max_value > 500)
{
return true;
}
Try
{
Thread.Sleep (10);
}catch (interruptedexception e)
{

}
System.out.println (Thread.CurrentThread (). GetName () + ": Method ..." +max_value++);
return false;
}
public void Change () throws Interruptedexception
{
Thread.Sleep (30);
if (FLAG1)
Flag1 = false;
Else
Flag2 = false;
}
}
public class Bank3 {
public static void Main (string[] args) throws Interruptedexception {
TICKETWINDOW3 TW3 = new TicketWindow3 ();
Tw3.change ();
thread T1 = new Thread (TW3);
Thread t2 = new Thread (TW3);
thread t3 = new Thread (TW3);
T1.start ();
T2.start ();
Tw3.change ();
T3.start ();
}
}


As we can see from the results, the synchronization of threads can still be ensured by locking two different class methods. This means that the above hypothetical class method is a lock in a smaller area of the class, is not, and the lock of the class method is also the this lock.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.