public class MultiThreadShareData {
public static void main(String[] args) {
ShareData data = new ShareData();
new Thread(()->{
for(int i=0;i<30;i++){
data.increment();
}
}).start();
new Thread(()->{
for (int j=0;j<20;j++) {
data.decrement();
}
}).start();
}
}
/**
封装共享数据的对象
*/
class ShareData{
//共享数据
private int j=0;
/**
对共享数据进行操作的方法
*/
public synchronized void increment(){
this.j++;
System.out.println(Thread.currentThread().getName()+":j增加了1后j等于 "+j);
}
public synchronized void decrement() {
this.j--;
System.out.println(Thread.currentThread().getName()+":j减少了1后j等于"+j);
}
public int getJ() {
return j;
}
}
运行结果:
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:D:\Java Web\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=53390:D:\Java Web\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\zhuomian\cdell\Desktop\FindSongs-master\out\production\FindSongs;D:\zhuomian\cdell\Desktop\FindSongs-master\lib\json-20180130.jar" MultiThreadShareData
Thread-0j增加了1之后等于1
Thread-0j增加了1之后等于2
Thread-0j增加了1之后等于3
Thread-0j增加了1之后等于4
Thread-0j增加了1之后等于5
Thread-0j增加了1之后等于6
Thread-0j增加了1之后等于7
Thread-0j增加了1之后等于8
Thread-0j增加了1之后等于9
Thread-0j增加了1之后等于10
Thread-0j增加了1之后等于11
Thread-0j增加了1之后等于12
Thread-0j增加了1之后等于13
Thread-0j增加了1之后等于14
Thread-0j增加了1之后等于15
Thread-1j减少1之后等于14
Thread-1j减少1之后等于13
Thread-1j减少1之后等于12
Thread-1j减少1之后等于11
Thread-1j减少1之后等于10
Thread-1j减少1之后等于9
Thread-1j减少1之后等于8
Thread-1j减少1之后等于7
Thread-1j减少1之后等于6
Thread-1j减少1之后等于5
Thread-1j减少1之后等于4
Thread-1j减少1之后等于3
Thread-1j减少1之后等于2
Thread-1j减少1之后等于1
Thread-1j减少1之后等于0
Thread-1j减少1之后等于-1
Thread-1j减少1之后等于-2
Thread-1j减少1之后等于-3
Thread-1j减少1之后等于-4
Thread-1j减少1之后等于-5
Thread-0j增加了1之后等于-4
Thread-0j增加了1之后等于-3
Thread-0j增加了1之后等于-2
Thread-0j增加了1之后等于-1
Thread-0j增加了1之后等于0
Thread-0j增加了1之后等于1
Thread-0j增加了1之后等于2
Thread-0j增加了1之后等于3
Thread-0j增加了1之后等于4
Thread-0j增加了1之后等于5
Thread-0j增加了1之后等于6
Thread-0j增加了1之后等于7
Thread-0j增加了1之后等于8
Thread-0j增加了1之后等于9
Thread-0j增加了1之后等于10
Process finished with exit code 0