zoukankan      html  css  js  c++  java
  • 11.18

    public class PCTest {
      public static void main(String[] args) {
        Share s=new Share();
        Producer p=new Producer(s,1);
        Consumer c=new Consumer(s,1);
        p.start();
        c.start();
      }
    }

    运行结果如下:

    生产者1  输出的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    消费者1  得到的数据为:0
    生产者1  输出的数据为:1
    生产者1  输出的数据为:2
    生产者1  输出的数据为:3
    生产者1  输出的数据为:4
    生产者1  输出的数据为:5
    生产者1  输出的数据为:6
    生产者1  输出的数据为:7
    生产者1  输出的数据为:8
    生产者1  输出的数据为:9

    public class SimpleRunnable implements Runnable {
        public void run() { // 覆盖run()方法
            int i = 15;
            while (i-- >= 1) { // 循环15次
                try {
                    System.out.print("*");
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) {
            Thread thread1 = new Thread(new SimpleRunnable(),"线程1"); // 创建线程1
            thread1.start(); // 启动线程1
        }
    }

    运行结果如下:

    ***************

    public class SimpleThread extends Thread {
        public SimpleThread(String name) {        // 参数为线程名称
            setName(name);
        }
        public void run() {        // 覆盖run()方法
            int i = 0;
            while (i++ < 5) {         // 循环5次
                try {
                    System.out.println(getName() + "执行步骤" + i);
                    Thread.sleep(1000);     // 休眠1秒
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) {
            SimpleThread thread1 = new SimpleThread("线程1");     // 创建线程1
            SimpleThread thread2 = new SimpleThread("线程2");     // 创建线程2
            thread1.start();                                     // 启动线程1
            thread2.start();                                    // 启动线程2
        }
    }

    运行结果如下:

    线程2执行步骤1
    线程1执行步骤1
    线程2执行步骤2
    线程1执行步骤2
    线程2执行步骤3
    线程1执行步骤3
    线程2执行步骤4
    线程1执行步骤4
    线程2执行步骤5
    线程1执行步骤5

    public class SyncThread extends Thread {
        private char cha;
        public SyncThread(char cha) {                         // 构造函数
            this.cha = cha;
        }
        public void run() {
            PrintClass.printch(cha);                         // 调用同步方法
            System.out.println();
        }
        public static void main(String[] args) {
            SyncThread t1 = new SyncThread('A');             // 创建线程A
            SyncThread t2 = new SyncThread('B');             // 创建线程B
            t1.start();                                     // 启动线程A
            t2.start();                                     // 启动线程B
        }
    }
    class PrintClass {
        public static synchronized void printch(char cha) { // 同步方法
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);                    // 打印一个字符休息1秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.print(cha);
            }
        }
    }

    运行结果如下:

    AAAAA
    BBBBB

    public class Water {
        boolean water = false;                            // 反应水塘状态的变量
        public boolean isEmpty() {                            // 判断水塘是否无水的方法
            return water ? false : true;
        }
        public void setWater(boolean haveWater) {            // 更改水塘状态的方法
            this.water = haveWater;
        }
        public static void main(String[] args) {
            Water water=new Water();                        // 创建水塘对象
            ThreadA threadA = new ThreadA(water);            // 创建进水线程
            //ThreadB threadB = new ThreadB(water);            // 创建排水线程
            //threadB.start();                                // 启动排水线程
            threadA.start();                                // 启动进水线程
        }
    }

    运行结果如下:

    开始进水.....
    1分钟
    2分钟
    3分钟
    4分钟
    5分钟
    进水完毕,水塘水满。

  • 相关阅读:
    【贪心算法】POJ-3040 局部最优到全局最优
    【贪心算法】POJ-1017
    【贪心算法】POJ-2393 简单贪心水题
    【贪心算法】POJ-3190 区间问题
    项目选题报告(团队)
    结对项目第一次作业——原型设计
    第三次作业团队展示
    软工实践第二次作业——数独
    软件工程实践第一次作业--准备
    字符串
  • 原文地址:https://www.cnblogs.com/cdl-sunshine/p/14152947.html
Copyright © 2011-2022 走看看