zoukankan      html  css  js  c++  java
  • Java测试守护线程的代码




    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.util.Scanner;


    class DaemonThread implements Runnable{


    @Override
    public void run() {
    System.out.println("进入守护线程"+Thread.currentThread().getName());
    try {
    writeToFile();
    } catch (Exception e) {

    e.printStackTrace();
    }
    System.out.println("退出守护线程"+Thread.currentThread().getName());

    }
    void writeToFile() throws Exception{
    File filename = new File("D:"+File.separator + "daemon.txt");
    OutputStream os = new FileOutputStream(filename,true);
    int count = 0;
    while(count<999){
    os.write(("\r\nword"+count).getBytes());
    System.out.println("守护线程"+Thread.currentThread().getName()+
    "向文件中写入了word"+count++);
    Thread.sleep(1000);
    }

    }
    }
    public class DaemonThreadDemo {


    public static void main(String[] args) {
    System.out.println("程序进入了主线程"+Thread.currentThread().getName());
    DaemonThread daemonThread = new DaemonThread();
    Thread thread = new Thread(daemonThread);
    thread.setDaemon(true);
    thread.start();

    Scanner sc = new Scanner(System.in);
    sc.next();
    System.out.println("退出主线程"+Thread.currentThread().getName());


    }


    }
  • 相关阅读:
    smarty模板中如何嵌入javascript脚本
    正则表达式(一)
    c#获取凌晨时间
    启动VUE项目报错:Error: Cannot find module 'node-sass'
    安装VUE过程记录
    Jenkins自动化工具完整介绍
    使用VS开发C#的常用快捷键
    获取枚举中的描述值
    PDF链接转字节流输出
    MSSQL执行计划的优化建议
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/6444553.html
Copyright © 2011-2022 走看看