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