编程语言
首页 > 编程语言> > Java3个编程题整理

Java3个编程题整理

作者:互联网

1.代码实现单例模式中的懒汉模式

/**
 * 单例模式中的懒汉模式
 */
public class LazySingleton {

    /**
     * 用volatile 修饰类变量
     */
    private static volatile LazySingleton singleton;

    /**
     * 构造器私有
     */
    private LazySingleton(){}

    /**
     * 双重检查
     * @return
     */
    public static LazySingleton getInstance(){
        if(singleton == null){
            synchronized (LazySingleton.class){
                if(singleton == null){
                    singleton = new LazySingleton();
                }
            }
        }
        return singleton;
    }


    /**
     * 测试
     */
    public static void main(String[] args) {
        LazySingleton singleton1 = LazySingleton.getInstance();
        LazySingleton singleton2 = LazySingleton.getInstance();
        System.out.println("singleton1:"+singleton1);
        System.out.println("singleton2:"+singleton2);
        System.out.println(singleton1 == singleton2);
    }

}

2.编写一个Java程序在屏幕上输出1!+2!+3!+...+10!的和

public class Factorial {

    /**
     * 用遍历的方式解题
     */
    public static void traverseMethod() {
        int i, j, mul, sum = 0;
        for (i = 1; i <= 10; i++) {
            mul = 1;
            for (j = 1; j <= i; j++) {
                mul = mul * j;
            }
            sum = sum + mul;
        }
        System.out.println("1!+2!+3!+...+10!=" + sum);
    }

    /**
     * 用递归的方式解题
     */
    public static void factorialMethod() {
        System.out.println("1!+2!+3!+...+10!="
                + (factorial(1) + factorial(2)
                + factorial(3) + factorial(4)
                + factorial(5) + factorial(6)
                + factorial(7) + factorial(8)
                + factorial(9) + factorial(10))

        );
    }

    public static int factorial(int num) {
        if (num == 1) {
            return 1;
        }
        return num * factorial(num - 1);
    }


    /**
     * 测试
     */
    public static void main(String[] args) {
        traverseMethod();//4037913
        factorialMethod();//4037913
    }

}

3.设计一个生产电脑和搬运电脑类,要求生产一台电脑就搬走一台电脑,如果没有新的电脑生产出来,则搬运工要等待新电脑产出;如果生产出的电脑没有搬走,则要等待电脑搬走之后在生产,并统计出生产的电脑数量。

public class ComputerTest {

    public static void main(String[] args) throws InterruptedException {
        Computer computer = new Computer();

        Thread produceThread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    computer.produceComputer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "produceThread");
        produceThread.start();
        Thread carryThread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    computer.carryComputer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "carryThread");
        carryThread.start();

        produceThread.join();
        carryThread.join();
        
        System.out.printf("共生产了%s台电脑\n", computer.getComputerCount());
    }

    /**
     * 电脑类
     */
    public static class Computer {
        /**
         * 生产出的电脑的数量
         */
        private int computerCount;
        //电脑是否不为空 true-不为空 false为空
        private volatile boolean flag;

        /**
         * 获取生产出的电脑的数量
         *
         * @return
         */
        public int getComputerCount() {
            return computerCount;
        }

        /**
         * 生产电脑
         */
        public synchronized void produceComputer() throws InterruptedException {
            while (flag) {
                wait();
            }
            this.computerCount++;
            flag = true;
            System.out.printf("%s 开始生产电脑\n", Thread.currentThread().getName());
            notifyAll();
        }

        /**
         * 搬运电脑
         */
        public synchronized void carryComputer() throws InterruptedException {
            //让搬运电脑消耗1秒钟,看程序执行效果
            TimeUnit.SECONDS.sleep(1);
            while (!flag) {
                wait();
            }
            flag = false;
            System.out.printf("%s 开始搬运电脑\n", Thread.currentThread().getName());
            notifyAll();
        }

    }

}

标签:Thread,电脑,编程,LazySingleton,System,static,Java3,整理,public
来源: https://www.cnblogs.com/zhaoran8775/p/13071919.html