第九天 设计一个方法,执行一次,一个线程对j加1,另外一个对j减一
2017-05-18 00:32
253 查看
public class Q55_3 {
public static void main(String[] args) {
Q55_3.test t=new Q55_3().new test();
t.a();
}
int j=0;
public synchronized int incre(){
System.out.println(j);
return j++;
}
public synchronized int desce(){
System.out.println(j);
return j--;
}
class test{
public void a(){
for(int i=0;i<2;i++){
new Thread(new Runnable(){
public void run(){incre();}
}).start();
new Thread(){
public void run(){desce();}
}.start();
}
}
}
}
public static void main(String[] args) {
Q55_3.test t=new Q55_3().new test();
t.a();
}
int j=0;
public synchronized int incre(){
System.out.println(j);
return j++;
}
public synchronized int desce(){
System.out.println(j);
return j--;
}
class test{
public void a(){
for(int i=0;i<2;i++){
new Thread(new Runnable(){
public void run(){incre();}
}).start();
new Thread(){
public void run(){desce();}
}.start();
}
}
}
}
相关文章推荐
- 异或加密法 在对文本进行简单加密的时候,可以选择用一个n位的二进制数,对原文进行异或运算。 解密的方法就是再执行一次同样的操作。
- 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法
- 多线程线程池控制一个方法的并发量 限制只有5个线程执行任务
- 【PE】判断一个线程是否执行结束的两种方法
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数(方法一)
- 每隔一段时间自动执行一次某个方法(使用线程)[C#]
- 线程操作-将一个函数在主线程执行的四种方法
- 如何等一个线程执行完执行另外一个线程,借助FutureTask类
- 另外,Volley中所有的请求都在一个额外线程中执行,而不会阻塞你的“主线程”。
- 每隔一段时间自动执行一次某个方法(使用线程)
- 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法
- 将一个函数在主线程执行及子线程执行的方法
- java类中增加了新方法,该方法掉用了另外一个类中没有的方法,在不执行该方法的情况下,执行会不会报错
- 在一个方法执行完后开启一个线程
- 在主函数main()之后再执行另外一个函数的方法
- 设计技巧17:Active Object 传递给一个线程命令,顺序执行传递的命令
- Monitor线程操作(当一个线程中处于等待状态时,另外一个线程来解锁它的等待状态继续执行下去)
- 怎样实现android 返回到上一个Activity并重新执行一次onCreate方法
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数(方法二)
- [C#]一个简易的、轻量级的方法并行执行线程辅助类