java-四个线程修改一个变量
2024-10-16 06:07:50
package 经典; public class 四个线程 { private int j=10; /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub 四个线程 mainClass=new 四个线程(); for(int i=2; i>0; i--)
{
Thread thread=new Thread(mainClass.new IncThread());
thread.start();
thread=new Thread(mainClass.new DecThread());
thread.start();
} } private synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+", j="+j);
} private synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+", j="+j);
} class IncThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for(int i=20; i>0 ;i--)
inc();
} } class DecThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for(int i=20; i>0 ;i--)
dec();
}
} }
最新文章
- hdu2302(枚举,大数取模)
- mac与php环境
- mysql完整备份时过滤掉某些库
- AtomicInteger源码分析
- SQL基础教程
- c++中的243、251、250错误原因
- 个推推送Android问题检测
- C#_deepCopy
- Portal相关技术及架构
- POJ 1845 Sumdiv(因子分解+快速幂+二分求和)
- 大型分布式C++框架《二:大包处理过程》
- js实现页面重定位的几种方法
- 【Vue 入门】使用 Vue2 开发一个展示项目列表的应用
- Mac上安装openCV(Java版本)
- CCF-CIDR合并-201812-3
- js实现复制内容到粘贴板
- SpringMVC中的一些注解
- 磁盘测试工具FIO工具安装和使用方法
- 1.0 poi单元格合合并及写入
- Qt 常用类 (4)—— QPoint