zoukankan      html  css  js  c++  java
  • 第16周作业

    题目1:

    编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。

    代码:

    import java.util.Date;
    
    public class test extends Thread {
        public void run() {                              
            Date date;
            while(true){
                date = new Date();
                System.out.println(date);                
                try {
                    sleep(10000);                         
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) {
            test time = new test();
            time.start();
        }
    }
     

     

    运行

    题目2:编写一个应用程序,利用Java多线程机制,实现猜数字游戏(随机数范围0~100之间的整数)。

    题目1:编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。

    代码

    1.ThreadObject.java

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    /**
     * 创建线程类,定义date对象
     */
    import java.util.Date;
    public class ThreadObject implements Runnable{
        public void run() {
            Date date;
            while(true){
                date = new Date();
                System.out.println(date);
                try {
                    Thread.sleep(1000);   //将线程进入休眠
                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 
            }
             
        }
         
     
    }

    2.Test.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class Test {
        public static void main(String[] args) {
            Thread time = new Thread(new ThreadObject()); //声明线程对象
            time.start();       //时time线程进入等待队列
         
     
        }
     
    }

    运行结果

  • 相关阅读:
    golang的select典型用法
    vscode配置git和提交代码到github教程
    VsCode中好用的git源代码管理插件GitLens
    GoMock框架使用指南
    golang对结构体排序,重写sort
    Go语言开发Prometheus Exporter示例
    golang 字符串拼接性能比较
    golang中的strings.Compare
    各大厂分布式链路跟踪系统架构对比
    NV triton启动方式说明
  • 原文地址:https://www.cnblogs.com/When6/p/12077874.html
Copyright © 2011-2022 走看看