Java Learning ———— Multi-threaded synchronization

Source: Internet
Author: User
public class Tongywo {public static void main (string[] args) {//TODO auto-generated method StubShangdian0 sd=new Shangdia N0 (100); Goumai0 no1=new Goumai0 (SD, "the most"); Goumai0 no2=new Goumai0 (n, SD, "No.2"); No1.start (); No2.start ();}} Class Shangdian0{int kucun=0;public Shangdian0 (int kucun) {this.kucun=kucun;} Public synchronized void Goumai (int i) {if (I<kucun) {kucun-=i; System.out.println (Thread.CurrentThread (). GetName () + "purchase" +i+ "this"); System.out.println ("store remainder" +kucun+ "Ben");} ELSE{SYSTEM.OUT.PRINTLN ("Insufficient Stock");}}} Class Goumai0 extends Thread{int i; Shangdian0 sd;public Goumai0 (int i, Shangdian0 SD, String name) {this.i=i;this.sd=sd;this.setname (name);} public void Run () {Sd.goumai (i);}}

Java Learning ———— Multi-threaded synchronization

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.