线程、内部类、文件输出

 分别用继承Thread和实现Runnable两种方式定义线程,两种内部类,调用时外部类的对象访问。

以下demo测试了三个线程,打印内容用文件作为控制台展示。

定义线程的两种方式

  1. 继承thread类
  • 继承thread类
  • 重写run方法
  • 调用start方法启动线程
  1. 实现runnable接口
  • 定义类实现runnable接口
  • new Thread(runnable实现类)
  • 调用start方法启动线程

 

线程的关闭: 通过标记flag关闭线程

/**
 * @author cuijiao
 *
 */
public class MutiThread {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // 1.Thread
        MutiThread mt = new MutiThread();
        Thread myThread = mt.new MyTread("thread1");// 只能通过外部类的对象访问非静态内部类
        myThread.start();

        // 2.Runnable
        Runnable myRun = mt.new MyRunnable();
        Thread thread = new Thread(myRun, "run2");
        thread.start();

        // 3.主线程
        int i = 0;
        boolean flag = true;
        while (flag) {
            FileConsole.print(Thread.currentThread().getName() + i);
            i++;
            if (i >= 50) {// 通过标记flag关闭线程
                flag = false;
            }
        }

    }

    // 1.继承Thread
    private class MyTread extends Thread {

        public MyTread(String name) {
            super(name);
        }

        @Override
        public void run() {
            int i = 0;
            while (true) {
                FileConsole.print(Thread.currentThread().getName() + i);
                i++;
            }
        }

    }

    // 2.实现Runnable接口
    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            int i = 0;
            while (true) {
                FileConsole.print(Thread.currentThread().getName() + i);
                i++;
            }
        }

    }
}

其中打印代码为:


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author cuijiao
 *
 */
public class FileConsole {
    /**
     * 用文件充当控制台(可查看行数无限制)
     *
     * @param str
     */
    public static void print(String str) {
        File file = new File("E:\\console.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(file, true);// 可追加内容
            bw = new BufferedWriter(fw);
            bw.write(str);
            bw.newLine();// 换行
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                    bw = null;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

进程:系统资源分配的最小单位 

线程:系统运行的最小单位,一个进程至少包含一个线程。

java程序:至少包含两个线程:主线程,GC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值