zoukankan      html  css  js  c++  java
  • 55 加一线程与减一线程共同操作一个数

     注意:不能synchronized(j) 因为j是基本数据类型,不是对象!

    package one;
    /**
     * 加一线程与减一线程共同操作一个数 两个问题: 1、线程同步--synchronized 2、线程之间如何共享同一个j变量--内部类
     * 
     */
    public class test {
        private int j;
    
        private synchronized void inc() {
            j++;
            System.out.println(Thread.currentThread().getName() + "-inc:" + j);
        }
    
        private synchronized void dec() {
            j--;
            System.out.println(Thread.currentThread().getName() + "-dec:" + j);
        }
    
        class Inc implements Runnable {
            public void run() {
                for(int i=0;i<500;i++){
                    inc();
                }
            }
        }
    
        class Dec implements Runnable {
            public void run() {
                for(int i=0;i<500;i++){
                    dec();
                }
            }
        }
    
        public static void main(String[] args) {
            test t = new test();
            Inc inc = t.new Inc();
            Dec dec = t.new Dec();
            for (int i = 0; i < 2; i++) {
                Thread thread = new Thread(inc);
                thread.start();
                thread = new Thread(dec);
                thread.start();
            }
    
        }
    }

     或者将加1减1方法放在内部类中:

    /**
     * 加一线程与减一线程共同操作一个数 两个问题: 1、线程同步--synchronized 2、线程之间如何共享同一个j变量--内部类
     * 
     */
    public class test {
        private int j;
    
        class Inc implements Runnable {
            public void run() {
                for(int i=0;i<500;i++){
                    inc();
                }
            }
            private synchronized void inc() {
                j++;
                System.out.println(Thread.currentThread().getName() + "-inc:" + j);
            }
    
        }
    
        class Dec implements Runnable {
            public void run() {
                for(int i=0;i<500;i++){
                    dec();
                }
            }
            private synchronized void dec() {
                j--;
                System.out.println(Thread.currentThread().getName() + "-dec:" + j);
            }
            
        }
    
        public static void main(String[] args) {
            test t = new test();
            Inc inc = t.new Inc();
            Dec dec = t.new Dec();
            for (int i = 0; i < 2; i++) {
                Thread thread = new Thread(inc);
                thread.start();
                thread = new Thread(dec);
                thread.start();
            }
    
        }
    }
  • 相关阅读:
    ISO/IEC 9899:2011 条款6.10.3——宏替换
    ISO/IEC 9899:2011 条款6.10.2——源文件包含
    关于Objective-C新增的__kindof关键字
    ISO/IEC 9899:2011 条款6.10.1——条件包含
    ISO/IEC 9899:2011 条款6.10——预处理指示符
    ISO/IEC 9899:2011 条款6.9.2——外部对象定义
    Objective-C中使用不定参数个数的方法调用
    php添加数据到xml文件的例子
    nginx rewrite重写与防盗链配置
    nginx url自动加斜杠的问题
  • 原文地址:https://www.cnblogs.com/seven7seven/p/3913585.html
Copyright © 2011-2022 走看看