4 Sample thread, 2 on the same number adder thread further 2 on the same shared number subtraction thread

Source: Internet
Author: User

Package com.threeti.mecool.web;

Detailed address: Method of subtraction
public class Addjian {


public int i=0;

Public synchronized void Add (String threadname) {
i++;
SYSTEM.OUT.PRINTLN (threadname+ "addition operation:" +i);
}

Public synchronized void Jian (String threadname) {
i--;
System.out.println (threadname+ "subtraction operation:" +i);
}

}


Package com.threeti.mecool.web;

Shared Data plus 10 threads
public class Addthread implements runnable{
Private Addjian Addjian;
Private String ThreadName;
Public Addthread (Addjian addjian,string threadname) {
This.addjian=addjian;
This.threadname=threadname;
}
@Override
public void Run () {
TODO auto-generated Method Stub
for (int i = 0; i < i++) {
Addjian.add (ThreadName);
}
}


}



Package com.threeti.mecool.web;

Shared data minus 10 threads
public class Jianthread implements runnable{
Private Addjian Addjian;
Private String ThreadName;
Public Jianthread (Addjian addjian,string threadname) {
This.addjian=addjian;
This.threadname=threadname;
}
@Override
public void Run () {
TODO auto-generated Method Stub
for (int i = 0; i < i++) {
Addjian.jian (ThreadName);
}
}


}


Package com.threeti.mecool.web;


public class Test {


/**
* @param args
*/
public static void Main (string[] args) {
Same Numeric object
Addjian addjian=new Addjian ();
2 threads to the same number addition operation
Thread Thread1=new Thread (new Addthread (Addjian, "thread-01"));
Thread Thread2=new Thread (new Addthread (Addjian, "thread-02"));
Thread1.start ();
Thread2.start ();

2 threads to the same number subtraction operation
Thread Thread3=new Thread (new Jianthread (Addjian, "thread-03"));
Thread Thread4=new Thread (new Jianthread (Addjian, "thread-04"));
Thread3.start ();
Thread4.start ();
}


}

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

4 Sample thread, 2 on the same number adder thread further 2 on the same shared number subtraction thread

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.