栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程练习题(12A34B......)(ABAB......)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程练习题(12A34B......)(ABAB......)

1.

创建两个线程,一个线程打印1---25
一个线程打印A-Z
输出效果是12A34B56C......5152Z

第一版不符合我们的要求(因为无法确定哪个线程先执行):

public class MyTest {
    public static void main(String[] args) {
        // 创建两个线程,一个线程打印 1---52
        //一个线程打印 A-Z
        //输出效果是 12A34B56C.......5152Z
        //第一版不是很符合我们的要求
        MyObject obj = new MyObject();
        NumberRunnable numberRunnable = new NumberRunnable(obj);
        CharRunnable charRunnable = new CharRunnable(obj);
        Thread th1 = new Thread(numberRunnable);
        Thread th2 = new Thread(charRunnable);

        th1.start();
        th2.start();

    }
}

class MyObject{

}
class NumberRunnable implements Runnable{
    private MyObject obj;
    public NumberRunnable(MyObject obj) {
        this.obj = obj;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 52; i++) {
            synchronized (obj){
                System.out.println(i); //1 2
                if(i%2==0){
                    obj.notify(); //唤醒等待的线程
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        }
    }
}
class CharRunnable implements Runnable{

    private MyObject obj;

    public CharRunnable(MyObject obj) {

        this.obj = obj;
    }

    @Override
    public void run() {
        for (int i ='A'; i <='Z'; i++) {
            synchronized (obj){
                System.out.println((char) i);
                obj.notify();
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

定义一个类型作为标记:

public class MyTest {
    public static void main(String[] args) {
        // 创建两个线程,一个线程打印 1---52
        //一个线程打印 A-Z
        //输出效果是 12A34B56C.......5152Z
        MyObject obj = new MyObject();
        NumberRunnable numberRunnable = new NumberRunnable(obj);
        CharRunnable charRunnable = new CharRunnable(obj);
        Thread th1 = new Thread(numberRunnable);
        Thread th2 = new Thread(charRunnable);
        th2.start();
        th1.start();


    }
}



class MyObject{
    // boolean  flag;
   public int flag=1; //int 类型作为标记,可以多标记一下情况
}

class NumberRunnable implements Runnable{

    private MyObject obj;

    public NumberRunnable(MyObject obj) {

        this.obj = obj;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 52; i++) {
            synchronized (obj){
                if(obj.flag!=1){ //1!=1 false
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //开始打印
                System.out.println(i);
                if(i%2==0){
                    obj.flag=2;
                    obj.notify(); //唤醒等待的线程
                }

            }

        }
    }
}

class CharRunnable implements Runnable{

    private MyObject obj;

    public CharRunnable(MyObject obj) {

        this.obj = obj;
    }

    @Override
    public void run() {
        for (int i ='A'; i <='Z'; i++) {
            synchronized (obj){
                if(obj.flag!=2){ //1!=2 true
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println((char) i);
                //修改标记
                obj.flag=1;
                //唤醒等待的线程
                obj.notify();
            }
        }
    }
}

2.

两个线程,一个线程打印100个A,一个线程打印100个B
效果就是 AB AB AB AB......

public class MyTest {
    public static void main(String[] args) {
        //两个线程 一个线程打印100个A 1个线程打印 100个B
        //效果就是AB AB AB AB...
        MyObject obj = new MyObject();
        AThread th1 = new AThread(obj);
        BThread th2 = new BThread(obj);
        th2.start();
        th1.start();
        
    }
}

class MyObject {
    // boolean  flag;
    public int flag = 1; //int 类型作为标记,可以多标记一下情况
}

class AThread extends Thread {
    private MyObject obj;
    public AThread(MyObject obj) {
        this.obj = obj;
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (obj) {
                if (obj.flag != 1) {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("A");
                obj.flag = 2;
                obj.notify();
            }
        }
    }
}

class BThread extends Thread {
    private MyObject obj;
    public BThread(MyObject obj) {
        this.obj = obj;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (obj) {
                if (obj.flag != 2) {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("B");
                obj.flag = 1;
                obj.notify();
            }

        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号