The example of this article tells the use of Java thread synchronization. Share it for everyone for your reference. The specific analysis is as follows:
The use of multi -threading provides a lot of convenience for our procedures, and it also brings us troubles that we have never considered in the past. When we use multi -threaded shared resources, accidents will happen: for example, we go out to eat together, everyone is a thread, and the food on the table is shared resources. When I see the braised chicken legs on the table When I saw the goal, when I saw it, I suddenly ~~~ The chicken legs disappeared, and a thread closer to the plate was holding it proudly.
In order to avoid the occurrence of the above problems, Java provides us with "Synchronized (synchronization) modifier" to avoid resource conflict. You can declare a function or variable in the resource class as Synchronized, each inherited from Object from Object The classes contain a machine lock (LOCK), which is inherent for the rest of his life and does not need to write any code to enable it. When we call any synchronized function, the object will be locked, and all Synchronized functions in the object cannot be called until the first function is executed and the lock is unlocked.
Import java.awt.Borderlayout; Import Java.awt.event.Actionevent; Import Java.awt.actionListener; Import javax.swing.jbutton; Import ing.jframe; Import javax.swing.jpanel; Import javax.swing .Jscrollpane; Import Javax.swing.jtextarea; Import Javax.swing.jtextField;/*** thread synchronization* We simulate a bank storage procedure to prove the necessity of thread synchronization in Java*: Synch ronized The modification of the decoration is that the decoration is the account, the amount of bank deposit is displayed by the amount of bank deposit to display Private Xtarea Textarea = NULL; // The transaction process shows the private jbutton Button = NULL; // Start the button of the simulation transaction/ *** Construct a bank withdrawal interface*/ public testmain5 () {super ("thread synchronization test"); myaccounts = new myaccounts (); (( Myaccounts.inquire ()), 10); // The initial deposit in the bank is 100 textarea = new jtextarea (); textarea.setText ("Transaction Log:"); ); @utton = New Jbutton ("Starting Transaction"); Button.adDactionListener (New ActionListener () {Public Void ActionPerformed (ActionEvent E) {New Bank ("Clock Tower Sub -branch", MyAccounts, BANK.DEA L_saving, 800); New Bank ("High -tech Sub -branch" , Myaccounts, Bank.deal_saving, 1300); New Bank ("Xiaozhai Sub -branch", Myaccounts, Bank.Deal_Fetch, 200); New Bank ("Yanta Sub -branch", Myaccounts, Bank.deal_fetch, 400); ew bank (" Xingqing Sub -branch ", myaccounts, bank.deal_saving, 100); New Bank (" Tugen Sub -branch ", Myaccounts, Bank.Deal_Fetch, 700);}); ; Pane.add (Button); this.getContentPane (). add (Pane, Borderlayout.north); this.getContentpane (). ADD (spD (spD (sp); .Exit_on_close); this.Setsize (300, 200 ) ;SetLocationRelativeto (null); this.setvisible (true);} /*** bank trading hall class* General banks will have n trading halls. Features*/ Class Bank Extends Thread {/ ***Static field: It is used to indicate the storage*/ public static final into defining_saving = 0;/ ***Static field: It is used to indicate the extraction of*/ public static final. Etch = 1; Private int Buy = bank.deal_fetch; // default to use the withdrawal int count = 0; Private myaccounts myaccounts = null; // Construct this bank transaction hall* @param Na ME's name of this trading hall* @ Param myaccounts My bank account* @param Buy behavior, reference field: dead_saving or defer_fetch* @param count money number*/ public bank (string name, myaccounts myaccounts, in T buy, int count) {super (name); this. myaccounts = myaccounts; this.Buy = Buy; this.Count = count; this.start ();} public void run () {int $ count = 0; if (buy ==Deal_sa Ving) {// If it is deposit Business $ Count = MyAccounts.saving (Count);} Else if (buy == BankAl_fetch) {// If it is withdrawal business $ Count = MyAccounts.fetch (Count);} Text.SetText (Integer.Tostring ($ Count )); Textarea.append ("/N" + This.getName () + " + (Buy == Bankal_saving? $ Count);}} / *** My account number* Synchronous testing* / class myaccounts {Private Integer Count = 1100; Public Myaccounts () {} / *** 查 询* / Public Inq uire () {{{ Synchronized (Count) {Return Count;} / *** deposit business* @param C deposit number* @Return business after completion* / PUBLIC INT SAVING (INT C) {Synchronized (count) { // Return Count += C; // In order to better observe, we comment on this simple sentence to int $ Count = Inquire (); // First check the deposit in the account $ count += c; try {thread.sleep (thread.sleep (thread.sleep 1000); // In order to better observe, the business pauses here for 1 second} Catch (interruptedException ex) {ex.printstacktrace ();} count = $ count; // Return to the latest deposit number}/*** ACTO business* @Param C to withdraw money* @Return business after completion*/public int FETCH (int C) {synchronized (count) {// Return Countt -= c; // In order to better observe, we comment on this simple sentence to int $ Count = Inquire (); // First check the deposit in the account $ count -= c; try {thread.sleep (1000 1000 ); // In order to better observe, the business pauses here for 1 second} Catch (InterruptedException EX) {ex.printstacktrace ();} Count = $ count; / Return to the latest deposit number}} Public Static void main (string [] args) {new testmain5 ();}}It is hoped that this article is helpful to everyone's Java program design.