public class IncDecThread {
private int j=10;
/*
* 题目:用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1
* 两个问题:
* 1、线程同步--synchronized
* 2、线程之间如何共享同一个j变量--内部类
*/
public static void main(String[] args) {
IncDecThread incDec=new IncDecThread();
Inc inc=incDec.new Inc();
Dec dec=incDec.new Dec();
for(int i=0;i<2;i++){
Thread thread=new Thread(inc);
thread.start();
thread=new Thread(dec);
thread.start();
}
}
public synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
}
public synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
}
class Inc implements Runnable{
public void run(){
for(int i=0;i<20;i++){
inc();
}
}
}
class Dec implements Runnable{
public void run(){
for(int i=0;i<20;i++){
dec();
}
}
}
}
private int j=10;
/*
* 题目:用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1
* 两个问题:
* 1、线程同步--synchronized
* 2、线程之间如何共享同一个j变量--内部类
*/
public static void main(String[] args) {
IncDecThread incDec=new IncDecThread();
Inc inc=incDec.new Inc();
Dec dec=incDec.new Dec();
for(int i=0;i<2;i++){
Thread thread=new Thread(inc);
thread.start();
thread=new Thread(dec);
thread.start();
}
}
public synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
}
public synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
}
class Inc implements Runnable{
public void run(){
for(int i=0;i<20;i++){
inc();
}
}
}
class Dec implements Runnable{
public void run(){
for(int i=0;i<20;i++){
dec();
}
}
}
}

本文提供了一个Java多线程程序实例,展示了如何通过四个线程(两个进行加操作,两个进行减操作)来同步修改同一变量,并使用内部类实现线程间的变量共享。

被折叠的 条评论
为什么被折叠?



