首页 > 试题广场 > 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每
[问答题]
使用内部类实现线程设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。

5个回答

添加回答
public class Solution {
    
    
    class InnerSolu{
        int j = 5;
        
        void fun(){
            int[] var = {0};
            var[0] = j;
            
            for(int i=0; i< 4; i++){
                int[] index = {0};
                index[0] = i;
                if (i<2){
                    new Thread(() -> {
                        var[0] = var[0] + 1;
                        System.out.println(index[0]+" "+var[0]);
                    }).start();
                } else {
                    new Thread(() -> {
                        var[0] = var[0] - 1;
                        System.out.println(index[0]+" "+var[0]);
                    }).start();
                }
            }
        }
    }
    
    public static void main(String[] args){
        Solution solution = new Solution();
        Solution.InnerSolu innerSolu = solution.new InnerSolu();
        innerSolu.fun();
    }
    
}

发表于 2019-04-23 20:47:55 回复(0)
考察点:JAVA线程 参考回答: public class ThreadTest1{ private int j; public static void main(String args[]){ ThreadTest1 tt=new ThreadTest1(); Inc inc=tt.new Inc(); Dec dec=tt.new Dec(); for(int i=0;i<2;i++){ Thread t=new Thread(inc); t.start(); t=new Thread(dec); t.start(); } } 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<100;i++){ inc(); } } } class Dec implements Runnable{ public void run(){ for(int i=0;i<100;i++){ dec(); } } } }
发表于 2019-05-01 21:36:43 回复(0)

public class ThreadFourInner {

class InnerThread {
    int j = 5;

    void ThreadFour()

    {
        int[] a = {j};
        for (int i = 0; i < 4; i++) {
            if (i < 2) {
                new Thread(() -> {
                    increate(a);
                }
                ).start();
            } else {
                new Thread(() -> {
                    decreate(a);
                }).start();
            }
        }

    }

    synchronized void increate(int[] a) {
        a[0] += 1;
        System.out.println("线程 " + Thread.currentThread() + ":" + a[0]);

    }

    synchronized void decreate(int[] a) {
        a[0] -= 1;
        System.out.println("线程 " + Thread.currentThread() + ":" + a[0]);
    }

}

public static void main(String[] args) {
    ThreadFourInner threadFourInner = new ThreadFourInner();
    threadFourInner.new InnerThread().ThreadFour();

}

}

发表于 2019-04-30 16:17:49 回复(0)
链接:https://www.nowcoder.com/questionTerminal/f24ca2cf7c444915a19035d5d78ac71b?orderByHotValue=1&mutiTagIds=570&page=1&onlyReference=false
来源:牛客网
publicclassSolution {
     
     
    classInnerSolu{
        intj =5;
         
        voidfun(){
            int[] var = {0};
            var[0] = j;
             
            for(inti=0; i<4; i++){
                int[] index = {0};
                index[0] = i;
                if(i<2){
                    newThread(() -> {
                        var[0] = var[0] +1;
                        System.out.println(index[0]+" "+var[0]);
                    }).start();
                }else{
                    newThread(() -> {
                        var[0] = var[0] -1;
                        System.out.println(index[0]+" "+var[0]);
                    }).start();
                }
            }
        }
    }
     
    publicstaticvoidmain(String[] args){
        Solution solution =newSolution();
        Solution.InnerSolu innerSolu = solution.newInnerSolu();
        innerSolu.fun();
    }
     
}
发表于 2019-04-25 23:38:28 回复(0)
链接:https://www.nowcoder.com/questionTerminal/f24ca2cf7c444915a19035d5d78ac71b?orderByHotValue=1&mutiTagIds=570&page=1&onlyReference=false
来源:牛客网

public class cool {
     
     
    class cool{
        int s = 5;
         
        void fun(){
            int[] var = {0};
            var[0] =s;
             
            for(int i=0; i< 4; i++){
                int[] index = {0};
                index[0] = i;
                if (i<2){
                    new Thread(() -> {
                        var[0] = var[0] + 1;
                        System.out.println(index[0]+" "+var[0]);
                    }).start();
                } else {
                    new Thread(() -> {
                        var[0] = var[0] - 1;
                        System.out.println(index[0]+" "+var[0]);
                    }).start();
                }
            }
        }
    }
     
    public static void main(String[] args){
        cool cool = new cool();
      cool.cool cool = cool.new cool();
        cool.fun();
    }
     
}

发表于 2019-04-25 22:33:15 回复(0)