多线程测试题

多线程知识点

synchronized关键字的运用主要包括三方面:

锁代码块(锁对象可指定,可为this、XXX.class、全局变量)

public class Sync{
    private int a = 0;
    public void add(){  
        synchronized(this){
            System.out.println("a values " + ++a);
        }
    }
}

锁普通方法(锁对象是this,即该类实例本身)

public class Sync{
    private int a = 0;
    public synchronized void add(){ 
        System.out.println("a values " + ++a);
    }
}

锁静态方法(锁对象是该类,即XXX.class)

public class Sync{
    private static int a = 0;
    public synchronized static void add(){  
        System.out.println("a values " + ++a);
    }
}

参考博客:https://blog.youkuaiyun.com/lang_programmer/article/details/72722751

1设计4个线程、实现2个线程对i加一,2个线程对i进行减一

public class Test {
    private int i=0;
    private synchronized void add(){
        i++;
        System.out.println(Thread.currentThread().getName()+"加"+i);
    }
    private synchronized void sub(){
        i--;
        System.out.println(Thread.currentThread().getName()+"减"+i);
    }

    //继承Thread类
    class First extends Thread{
        public void run() {
            add();
        }
    }

    //继承Thread类
    class Second extends Thread{
        public void run() {
            sub();
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        First f = test.new First();
        Second s = test.new Second();

        for(int i = 0; i < 2; i++){
            Thread t = new Thread(f);
            t.start();
            Thread t1 = new Thread(s);
            t1.start();
        }
    }
}

2 编写线程、实现子线程先循环10次、接着主线程循环20、再接着子线程循环10次、主线程循环20次、反复进行50次

//要让他们交替进行,可用信号量控制,并用wait  ,notify 进行线程间通信
public class Test {
    public static void main(String[] args) {

        final MyThread threads=new MyThread();
        new Thread(
                new Runnable(){
                    public void run(){
                        for(int i=1;i<=50;i++){
                            threads.subThread(i);
                        }
                    }
                }
        ).start();
        new Thread(new Runnable(){
            public void run(){
                for(int i=1;i<=50;i++){
                    threads.mainThread(i);
                }
            }
        }).start();
    }
}

class MyThread{
    boolean bShouldSub=true;//标志子线程方法是否被调用
    public synchronized void subThread(int i){
        if(!bShouldSub){//若子线程没被调用,即主线程正在运行,所以等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for(int j=1;j<=10;j++){
            System.out.println("sub thread :"+i+",loop : "+j);
        }
        bShouldSub=false;//子线程运行完毕
        this.notify();//唤醒其他线程,即主线程
    }
    public synchronized void mainThread(int i){
        if(bShouldSub){//若子线程正在被调用,所以等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for(int j=1;j<=20;j++){
            System.out.println("main thread :"+i+",loop : "+j);
        }
        bShouldSub=true;//主线程调用完毕
        this.notify();//唤醒子线程
    }
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值