The application of Java synchronized

Source: Internet
Author: User

I've been knocked out by a thread these days.

Specially to make up for the Java thread knowledge


The Synchronized keyword is the ability to implement a class object that is called only by one thread at a time, and other threads to invoke this object only until the thread that is being called ends or stops (typically used in a stopped state and, if it is finished, it is easier to use the join () method directly) to get the object


Synchronized using method One: Locking Object methods

Write the code and write a mock ticket to the scene:

Write a tickets class first

Class Tickets {public tickets () {//TODO auto-generated constructor Stub}int total_tickets = 30;void sell_tickets (String t Hread_name) {synchronized (this) {total_tickets--; System.out.println (Thread_name + "sell a ticket, remains" + Total_tickets);}}
Set a total of 30 tickets,
Sell_tickets (String thread_name)
Will print out the thread name and the remaining number, note that sell_tickets () uses synchronized (this), which means that the code can only be called by one thread at a time

Thread counter1 = new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; I < 10; i++) {tickets.sell_tickets ("1"), try {thread.sleep (),} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}});  Thread counter2 = new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; I < 10; i++) {tickets.sell_tickets ("2"), try {thread.sleep (),} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}});  Thread Counter3 = new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; I < 10; i++) {tickets.sell_tickets ("3"), try {thread.sleep (),} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}); Counter1.start (); Counter2.start (); Counter3.start ();

Create a new 1 tickets class object, 3 counter threads, each thread sells 1 tickets at a certain time, and in Thread.Sleep it allows other waiting threads to gain tickets.sell_ticket rights.

The results are as follows:

1 sell a ticket, remains 293 sell a ticket, remains 282 sell a ticket, remains 271 sell a ticket, remains 262 sell a Ticke  T, remains 253 sell a ticket, remains 242 sell a ticket, remains 231 sell a ticket, remains 223 sell a ticket, remains 212 Sell a ticket, remains 201 sell a ticket, remains 193 sell a ticket, remains 182 sell a ticket, remains 171 sell a ticket , remains 163 sell a ticket, remains sell a ticket, remains 143 sell a ticket, remains 131 sell a ticket, remains 122 Sell a ticket, remains 113 sell a ticket, remains 101 sell a ticket, remains sell a ticket, remains Bayi sell a ticket, R Emains sell a ticket, remains sell a ticket, remains sell a ticket, remains sell a ticket, remains-sell a T Icket, remains, sell a ticket, remains sell a ticket, remains 0
As you can see, three threads call the tickets object and no conflict has occurred


Synchronized using method Two: locking objects in threads

This time we don't lock the tickets class method

Just a general definition.

Class Tickets {public tickets () {//TODO auto-generated constructor Stub}int total_tickets = 30;void sell_tickets (String t Hread_name) {total_tickets--; System.out.println (Thread_name + "sell a ticket, remains" + Total_tickets);}}

But we use synchronized to synchronize tickets objects in the runnable of the thread

Thread counter1 = new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; I < 10; i++) {synchronized (tickets) {tickets.sell_tickets ("1");} try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}});
The same effect can be achieved by synchronizing




The application of Java synchronized

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.