day23--Cinema ticket purchase Problem solving synchronous code block synchronization method Introduction of static synchronization method

Source: Internet
Author: User

Package cn.itcast_11;

/*
* A: Who is the lock object of the synchronization code block?
* Arbitrary objects.
*
* B: The format of the synchronization method and the problem of lock object?
* Add the Sync keyword to the method.
*
* Who is the lock of the synchronization method?
* This (a class itself is the object of this)
*
* C: Static method and lock object problem?
* Who is the lock object of the static method?
* Class of bytecode file object. (Reflection will Speak)
*/
public class Sellticketdemo {
public static void Main (string[] args) {
Create a Resource object
Sellticket st = new Sellticket ();

Create three thread objects
thread T1 = new Thread (ST, "Window 1");
Thread t2 = new Thread (ST, "Window 2");
thread t3 = new Thread (St, "Window 3");

Start thread
T1.start ();
T2.start ();
T3.start ();
}
}

Package cn.itcast_11;



/**
* This module uses the synchronous code block operation of the same code, no matter where the execution of the location, will be the same effect, the use of x%2==0 features, the path of the thread is divided into two sides, but the previous said, synchronous code block operation of the same code is not necessary to consider the location, because their locks are the same feature, This leads to the static synchronization method, and the non-static synchronization method. Because if and else in the effect is the same, if the effect is not the same, the content of the two code lock is inconsistent, as long as the lock is the same, you can achieve the same effect so that the synchronization method, and the static synchronization method requires what kind of lock.
*/

/**
* Synchronize code blocks: arbitrary objects, but object definitions cannot be within the parentheses of the code block, or each time a new object maintenance lock is created, the meaning of the lock is invalidated
* Synchronization method: The inner object of this class: this, the condition is implied.
* Static Synchronization method: This class of. class object, because static is executed before the class is initialized, so the normal object does not work as a lock, because static is created before it is created, classes created earlier than static, only classes loaded into the method area class
*/
public class Sellticket implements Runnable {

Define 100 Tickets
private static int tickets = 100;

Define the same lock
Private Object obj = new Object ();
Private Demo D = new demo ();

private int x = 0;

Synchronous code block with obj lock
@Override
public void Run () {
while (true) {
Synchronized (obj) {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
//                    }
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
//                }
//            }
//        }
//    }

Synchronous code block locks with arbitrary objects
@Override
public void Run () {
while (true) {
Synchronized (d) {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
//                    }
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
//                }
//            }
//        }
//    }

@Override
public void Run () {
while (true) {
if (x%2==0) {
Synchronized (Sellticket.class) {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
}
}
}else {
Synchronized (d) {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
//                        }
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
//                    }
//                }

Sellticket ();

}
x + +;
}
}
Indicates that any type can
private void Sellticket () {
Synchronized (d) {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
//            }
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
//            }
//        }
//    }

If a method goes in to see the code is synchronized, then I would like to be able to add this synchronization on the method?
Private synchronized void Sellticket () {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
//            }
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
//            }
//    }

private static synchronized void Sellticket () {
if (Tickets > 0) {
try {
Thread.Sleep (100);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName ()
+ "being sold" + (tickets--) + "Ticket");
}
}
}

Class Demo {
}

day23--Cinema ticket purchase Problem solving synchronous code block synchronization method Introduction of static synchronization method

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.