In Java, synchronization is an issue that needs to be taken into account when multiple ready-made simultaneous manipulation of the same resource. If the station ticket sales, the different tickets to sell the same shift tickets will be synchronized, otherwise the ticket will be a problem. The following code simulates the station selling tickets:
classTicketsellerImplementsrunnable{ Public intTicket = 20; Public voidrun () { for(inti = 0; I < 100; i++) { synchronized( This) { if( This. ticket-->0) {Utilities.sleep (300); System.out.println (Thread.CurrentThread (). GetName ()+ "sell one ticket, rest ticket:" + ( This. Ticket) + "Zhang"); } } } }}classutilities{ Public Static voidSleepintTimeSpan) { Try{thread.sleep (TimeSpan); } Catch(Exception e) {} }}classhello{ Public Static voidMain (string[] args) {Ticketseller ts=NewTicketseller (); Thread TL=NewThread (TS, "Tongliang Station"); Thread BS=NewThread (TS, "Bishan Station"); Thread SPB=NewThread (TS, "Shapingba Station"); Tl.start (); Bs.start (); Spb.start (); }}
Output Result:
However, sometimes excessive use of synchronization can cause program performance to degrade. In addition to excessive synchronization will also have more serious consequences: deadlock.
In other words, the synchronization code block to compete for the same resources, not to be. For example, in a narrow roadway in the direction of a thing, AB two cars meet, complementary concessions, the result is two cars can not go, this is the deadlock. The implication here is that a car occupies the east side of the road, it needs a B car to take the west side of the road, while the B car occupies the West road, it needs a car to let out a car occupied by the West Road. The two vehicles each occupy their own resources, and at the same time competing for the other's possession of resources, complementary to let, resulting in deadlock.
Here, to better illustrate and simulate the deadlock situation in Java. Let me cite one more example:
Xiao Hong and Xiao Ming will soon graduate, are preparing the composition of the graduation thesis. They use two books for their paper, one is "assembly language" and the other is "Introduction to Algorithms". They all went to the library to borrow the two books, but the library stipulated that one person could only borrow 1 books at a time. So Xiao Red borrowed the "assembly Principle", Xiao Ming borrowed "introduction to the algorithm." After a period of time, they finished half of the paper. At this time, Little Red needs Xiao Ming's hands of the "Introduction to the algorithm", and Xiao Ming also want to red hands of the "assembly principle." But Xiao Red said to Xiao Ming: "You first put the introduction to the algorithm" to me, I finished the paper after the "assembly principle" to you. Xiao Ming does not agree, he said to Xiao Red: "You put the" assembly principle "first to me, I completed the paper after the" Introduction to the algorithm "to you. "In this way, Xiao Hong and Xiao Ming each hold a book, but also asked to obtain the other's book to surrender their own possession of the book." The two of them were mutually exclusive, resulting in a deadlock.
Now I'm simulating the above scenario in the Java language:
classbook{ PublicString name; Public floatPrice ; PublicBook (String name,floatPrice ) { This. Name =name; This. Price =Price ; }}classStudentImplementsrunnable{PrivateString Studentname; PrivateBook Book1; PrivateBook Book2; PublicStudent (String studentname,book book1, book book2) { This. Book1 =Book1; This. Book2 =Book2; This. Studentname =Studentname; } Public voidrun () {synchronized( This. Book1) {System.out.println ( This. studentname+ "Got It" + This. Book1.name + "Start writing paper"); Utilities.sleep (5000); System.out.println ( This. studentname+ "Complete half needs" + This. Book2.name); synchronized( This. Book2) {System.out.println ( This. studentname+ "Got It" + This. Book2.name + "Continue writing paper"); } System.out.println ( This. studentname+ "completed the paper"); } }} classutilities{ Public Static voidSleepintTimeSpan) { Try{thread.sleep (TimeSpan); } Catch(Exception e) {System.out.println (e); } }}classhello{ Public Static voidMain (string[] args) {book Book1=NewBook ("Assembly Principle", 23.5f); Book Book2=NewBook ("Introduction to Algorithms", 85.5f); Thread XH=NewThread (NewStudent ("Little Red", Book1,book2)); Thread XM=NewThread (NewStudent ("Xiao Ming", Book2,book1)); Xh.start (); Xm.start (); }}
Output Result:
Shows that the program has been stuck in this place and will not execute down. This is the deadlock.
Questions about synchronization and deadlock in Java