Java实验四(不包含星号题)

1、两个线程实现奇数偶数计算

package Ex4.Firstquestion;

/**
 * @author 15328
 * 1、编写一个有两个线程的程序,
 * 第一个线程用来计算1~100之间的偶数及个数,
 * 第二个线程用来计算1-100之间的偶数及个数。
 */
public class Main {
    public static final int DELAY = 100;
    public static void main(String[] args) {
        Compute compute = new Compute(1, 100);
        Runnable task1 = () ->{
            try{
                compute.oddCompute(compute.from,compute.to);
                Thread.sleep((int)(DELAY*Math.random()));
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        };
        Runnable task2 = () ->{
            try{
                compute.evenCompute(compute.from,compute.to);
                Thread.sleep((int)(DELAY*Math.random()));
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        };
        new Thread(task1).start();
        new Thread(task2).start();
    }
}
class Compute{
    public int from = 1;
    public int to = 100;
    Compute(int from,int to){
        this.from = from;
        this.to = to;
    }
    public void oddCompute(int from,int to){
        //奇数
        int numOfOdd = 0;
        for(int i = from; i <= to; i++){
            if(i % 2 == 1) {
                numOfOdd++;
                System.out.println("第" + numOfOdd + "个奇数为:" + i);
            }
        }
    }
    public void evenCompute(int from,int to){
        //偶数
        int numOfEven = 0;
        for(int i = from; i <= to; i++){
            if(i % 2 == 0) {
                numOfEven++;
                System.out.println("第" + numOfEven + "个偶数为:" + i);
            }
        }
    }
}

在这里插入图片描述

1、一个线程实现奇数偶数计算

package Ex4.Firstquestion;

/**
 * @author 15328
 * 1、编写一个有两个线程的程序,
 *    第一个线程用来计算1~100之间的奇数及个数,
 *    第二个线程用来计算1-100之间的偶数及个数。
 *
 *
 *    一个线程实现
 */
public class NewMain {
    public static final int FROM = 1;
    public static final int TO = 100;
    public static void main(String[] args) {
        ComputeOfNew computeOfNew = new ComputeOfNew(FROM,TO);
        Thread thread = new Thread(computeOfNew);
        thread.start();
    }
}
class ComputeOfNew implements Runnable{
    public static int numOfOdd = 0;
    public static int numOfEven = 0;
    private int from;
    private int to;
    ComputeOfNew(int FROM,int TO){
        this.from = FROM;
        this.to = TO;
    }
    public void compute(int value){
        if(value % 2 != 0) {
            System.out.println("第 " + (++numOfOdd) + " 个奇数为:" + value);
        }
        else{
            System.out.println("第 " + (++numOfEven) + " 个偶数为:" + value);
        }
    }
    @Override
    public void run() {
        for(int i = this.from; i <= this.to; i++){
            compute(i);
        }
    }
}

在这里插入图片描述

2、补全代码

package Ex4.Secondquestion;

/**
 * @author 15328
 */
class Tortoise extends Thread
{
    int sleepTime = 0;
    int liveLength = 0;
    public Tortoise(String name,int sleepTime, int liveLength)
    {
        this.sleepTime=sleepTime;
        this.liveLength=liveLength;
        this.setName(name);
        // 设置线程的名字为name
    }
    @Override
    public void run()
    {
        while (true ) {
            liveLength--;
            System.out.println("@_@");
            try{
                Thread.sleep(sleepTime);
                // 让线程调用sleep()方法进入中断状态
            }
            catch (InterruptedException e) {

            }
            if (liveLength <= 0 ) {
                System.out.println(getName()+"进入死亡状态\n");
                //?
                break;
                // 结束run()方法的语句
            }
        }
    }
}
class Rabit extends Thread
{
    int sleepTime=0;
    int liveLength=0;
    public Rabit(String name,int sleepTime, int liveLength)
    {
        super(name);// 调用父类构造函数,设置线程的名字为name
        this.sleepTime=sleepTime;
        this.liveLength=liveLength;
    }
    @Override
    public void run()
    {
        while (true)
        {
            liveLength--;
            System.out.println("*_*");
            try{
                sleep( sleepTime);
            }
            catch (InterruptedException e) {}
            if (liveLength<=0)
            {
                System.out.println(getName() + "进入死亡状态\n");
                break;
            }
        }
    }
}
public  class ThreadExample
{
    public static void main(String a[])
    {
        Rabit rabit;
        rabit = new Rabit("rabit",15,55);
        // 新建线程rabit
        Tortoise tortoise = new Tortoise("tortoise",20,50);
        // 新建线程tortoise
        tortoise.start();  // 启动线程tortoise
        rabit.start();// 启动线程rabit
    }
}

在这里插入图片描述

posted @ 2024-02-16 16:43  在天边偷看小天使  阅读(9)  评论(0)    收藏  举报  来源