多线程:奇偶数分组打印

2020-04-23  本文已影响0人  啊了个支

一.基于wait,motify
package day26.homework;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.WildcardType;
import java.util.Random;
import day21.homework.News;

public class work3 {
// 在子线程中输出1-100之间的偶数,主线程输出1-100之间的奇数。
public static void main(String[] args) {
/*
* 创建和启动2个子线程,一个打印1-10之间奇数,一个打印1-10之间偶数, (1)要求每个线程要么不打印,要么就连续打印5个数,每个数打印间隔500毫秒
* (2)但两个线程不要求交替打印。
*/
// 创建所对象
Object object = new Object();
// 用同一把锁
work3Runable runable3 = new work3Runable(object);
work4Runable runable4 = new work4Runable(object);
Thread thread3 = new Thread(runable3, "偶数线程");
Thread thread4 = new Thread(runable4, "奇数线程");
// 开启各自线程
thread3.start();
thread4.start();
}

}

//输出偶数的线程
class work3Runable implements Runnable {
// 维持的锁对象
private Object obj;
// 是否已经遍历结束
private static boolean isend;

public static boolean isIsend() {
    return isend;
}

public work3Runable(Object obj) {
    this.obj = obj;
}

@Override
public void run() {
    // 获取同一个锁对象
    synchronized (obj) {
        // 获取线程名字
        String name = Thread.currentThread().getName();
        // 计数
        int count = 1;
        for (int i = 1; i < 100; i++) {
            // 偶数线程被唤醒
            if (!work4Runable.isIsend())
                obj.notify();

            if (i == 99) {
                // 偶数已经完结
                isend = true;
            }

            if (i % 2 == 0) {
                // 输出偶数
                System.out.println(name + ",第" + count++ + "个" + i);
                if (count == 6) {
                    // 到达5个,重置计数
                    count = 1;
                    // 分割线
                    System.out.println("=========================");

                    try {
                        // 休眠500毫秒
                        Thread.sleep(500);
                        // 如果奇数线程遍历数据还未结束,则线程等待,否则就不执行,只输出自己的
                        if (!work4Runable.isIsend() && !isend)
                            obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    System.out.println("~~~~~~~~~~~~~~偶数线程结束~~~~~~~~~~~~~~");
}

}

class work4Runable implements Runnable {
// 锁对象
private Object obj;
// 是否遍历结束
private static boolean isend;

public static boolean isIsend() {
    return isend;
}

public work4Runable(Object obj) {
    this.obj = obj;
}

@Override
public void run() {
    // 获取同一个锁对象
    synchronized (obj) {

        String name = Thread.currentThread().getName();
        // 计数
        int count = 1;

        for (int i = 1; i < 100; i++) {
            // 线程在这里被唤醒了
            obj.notify();
            // 奇数是随机的,要么不打印,要么打印
            Random rd = new Random();
            boolean flag = rd.nextBoolean();

            if (i == 99) {
                // 奇数已经完结
                isend = true;
            }

            if (i % 2 != 0) {
                System.out.println(name + ",第" + count++ + "个" + i);
                if (count == 6) {
                    count = 1;
                    System.out.println("=========================");
                    try {
                        Thread.sleep(500);
                        // 交替打印就去掉flag随机条件
                        if (flag && !work3Runable.isIsend() && !isend)
                            obj.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    System.out.println("~~~~~~~~~~~~~~奇数线程结束~~~~~~~~~~~~~~");
}

}

二。基于状态判断循环中断
package day26.homework;

import java.io.ObjectInputStream.GetField;
import java.lang.reflect.WildcardType;
import java.util.Random;

import day21.homework.News;

public class work2 {
// 在子线程中输出1-100之间的偶数,主线程输出1-100之间的奇数。
public static void main(String[] args) {
/*
* 创建和启动2个子线程,一个打印1-10之间奇数,一个打印1-10之间偶数, (1)要求每个线程要么不打印,要么就连续打印5个数,每个数打印间隔500毫秒
* (2)但两个线程不要求交替打印。
*/
Object object = new Object();
work5Runable runable3 = new work5Runable();
work6Runable runable4 = new work6Runable();
Thread thread3 = new Thread(runable3, "偶数线程");
Thread thread4 = new Thread(runable4, "奇数线程");
thread3.start();
thread4.start();
}

}

class work5Runable implements Runnable {
private static volatile int index = 0;
private static boolean isend = false;
private static boolean isbreak = false;

public static boolean isIsbreak() {
    return isbreak;
}

public static void setIsbreak(boolean isbreak) {
    work5Runable.isbreak = isbreak;
}

public static boolean isIsend() {
    return isend;
}

@Override
public void run() {
    String name = Thread.currentThread().getName();
    while (!isend) {
        synchronized ("fz") {
            // 如果是中断状态,什么都不做,等待
            if (!isbreak) {
                int count = 1;
                for (int i = index; i < 100; i++) {
                    index++;
                    if (i % 2 == 0) {
                        System.out.println(name + ",第" + count++ + "个" + i);
                        if (count == 6) {
                            count = 1;
                            System.out.println("=========================");
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            setIsbreak(new Random().nextBoolean());
                            if (isbreak && !work6Runable.isIsend()) {
                                break;
                            }
                        }
                    }
                }
                // 如果是临时中断出来,不认为是结束
                if (index >= 100) {
                    isbreak = true;
                    isend = true;
                }
                    
            }
        }
    }
    System.out.println("偶数线程结束");
}

}

class work6Runable implements Runnable {
private static boolean isend = false;
private static volatile int index = 0;
private static boolean isbreak = false;

public static boolean isIsend() {
    return isend;
}

@Override
public void run() {
    while (!isend) {
        synchronized ("fz") {
            // 如果偶数是中断状态
            if (work5Runable.isIsbreak()) {
                String name = Thread.currentThread().getName();
                int count = 1;

                for (int i = index; i < 100; i++) {
                    index++;
                    if (i % 2 != 0) {
                        System.out.println(name + ",第" + count++ + "个" + i);
                        if (count == 6) {
                            count = 1;
                            System.out.println("=========================");
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            
                            
                            if(!work5Runable.isIsend()) {
                                work5Runable.setIsbreak(false);
                                break;
                            }
                            
                        }
                    }
                }
                if (index >= 100) {
                    isbreak = true;
                    isend = true;
                }
                    
            }
        }

    }
    System.out.println("奇数线程结束");

}

}

三。基于yield()
class Even implements Runnable {

@Override
public void run() {
    // TODO Auto-generated method stub
    synchronized ("1") {
        int j = 1;
        for (int i = 0; i < 10; i++) {
            Thread.yield();
            
                
                for (int count = 0; count < 5; count++) {
                    
                    System.out.println(Thread.currentThread().getName() + j);
                    j += 2;
                    }
                
                System.out.println("-----------");      
        }
    }   
}

}

上一篇下一篇

猜你喜欢

热点阅读