Java------Multithreading (interactive threading)

Source: Internet
Author: User


1, the running result inflexible one, depends on the thread dispatch
2. Error occurs when thread execution is interrupted

3. Thread exclusion and critical zone management

1) The operating system has the following 3 scheduling principles for entering the respective critical section for several threads that share a variable:
2) at most one thread at a time can be within its critical section.
3) It is not possible for a thread to remain indefinitely in its critical section.
4) You cannot force a thread to wait indefinitely to enter its critical section. In particular, any thread entering the critical section does not interfere with the progress of other threads that are waiting to enter.

4. Synchronous statement synchronized (object)

Synchronous method Synchronized method declaration method declaration synchronized (this) {method body}

5. Deposit and withdrawal thread of bank account


Account.c

Package P25;public class Accounts {private String name;//depositor name private double balance;//account balance public (String name) {thi S.name = name;this.balance = 0;} Query balance public double balance () {return this.balance;} Public String GetName () {return this.name;} Deposit operation public void put (double value) {if (value > 0) {this.balance + = value;}} The withdrawal operation public double get (double value) {if (value > 0) {if (value < this.balance) {///Fetch this.balance = value;} else { Value = This.balance;this.balance = 0;} return value;} return 0;}} Class Save extends Thread {Private account account;private double value;public Save (account account, double value) {THIS.A Ccount = Account;this.value = value;} public void Run () {synchronized (account) {//Declare critical section, lock the current accounts object double Howmuch = This.account.balance ();//view balance try { Sleep (1);} catch (Interruptedexception e) {e.printstacktrace ();} This.account.put (this.value);d ouble howmuch2 = this.account.balance ();//View account balance System.out.println ( This.account.getName () + "account: Existing" + howmuch+ ", deposit"+ This.value +", the balance is "+ HOWMUCH2);}}} Class Fetch extends Thread {private account account;private double value;public Fetch (account account, double value) {This . Account = Account;this.value = value;} public void Run () {synchronized (account) {Double Howmuch = this.account.balance (); try {sleep (1);} catch (Interruptedexce Ption e) {//TODO auto-generated catch Blocke.printstacktrace ();} Double v = this.account.get (this.value);d ouble howmuch2 = This.account.balance (); System.out.println (This.account.getName () + "account: Existing" + howmuch+ ", remove" + V + ", balance" + Howmuch2);}}} Class Func extends Thread {private account account;private double value;public Func (account account, double value) {THIS.A Ccount = Account;this.value = value;} public void Run () {fun ();} synchronized void Fun () {Double howmuch = this.account.balance (); try {sleep (1);} catch (Interruptedexception e) { E.printstacktrace ();} This.account.put (this.value);d ouble v = this.account.get (this.value);d ouble howmuch2 = This.account.balance (); System.Out.println (This.account.getName () + "account: Existing" + Howmuch + ", take out" + V + ", balance" + Howmuch2);}} 
Bank.c

Package P25;public class Bank {public static void main (string[] args) {account Wang = new account ("Wang");(New Save (wang,1 ). Start ();(new Save (wang,200)). Start ();(new Fetch (wang,100)). Start (); Account li=new account ("Li");(new Save (li,300)). Start (); Account xu=new account ("Xu");(new Func (xu,100000)). Start ();}}


Note:You must declare a critical section, lock the account object, or you will get an error
Correct result:

Error Result:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java------Multithreading (interactive threading)

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.