慕课网_细说多线程之Thread VS Runnable

第1章 课前准备

1-1 前言 (00:49)

第2章 Thread VS Runnable

2-1 回顾线程创建的两种方式 (02:33)

继承Thread类

class MyThread extends Thread {
	public void run() {

	}
}

public class Hello {
	public static void main(String[] args) {
		MyThread mt = new MyThread();// 创建线程
		mt.start();// 启动线程
	}
}

实现Runnable接口

class MyThread implements Runnable {
	public void run() {

	}
}

public class Hello {
	public static void main(String[] args) {
		MyThread mt = new MyThread();
		Thread td = new Thread(mt);// 创建线程
		td.start();// 启动线程
	}
}

2-2 应用Thread模拟卖票 (07:54)

class MyThread extends Thread {
	private int ticketsCont = 5;// 一共有5张票
	private String name;

	public MyThread(String name) {
		super();
		this.name = name;
	}

	public void run() {
		while (ticketsCont > 0) {
			ticketsCont--;
			System.out.println(name + "卖了1张票,剩余票数为:" + ticketsCont);
		}
	}
}

public class TicketsThread {
	public static void main(String[] args) {
		// 创建三个线程,模拟三个窗口卖票
		MyThread mt1 = new MyThread("窗口1");
		MyThread mt2 = new MyThread("窗口2");
		MyThread mt3 = new MyThread("窗口3");

		// 启动这三个线程,也即是窗口,开始卖票
		mt1.start();
		mt2.start();
		mt3.start();
	}
}

2-3 应用Runnable模拟卖票 (09:08)

class MyThread implements Runnable {
	private int ticketsCont = 5;// 一共有5张票

	public void run() {
		while (ticketsCont > 0) {
			ticketsCont--;
			System.out.println(Thread.currentThread().getName() + "卖了1张票,剩余票数为:" + ticketsCont);
		}
	}
}

public class TicketsRunnable {
	public static void main(String[] args) {
		MyThread mt = new MyThread();
		// 创建三个线程来模拟三个售票窗口
		Thread th1 = new Thread(mt, "窗口1");
		Thread th2 = new Thread(mt, "窗口2");
		Thread th3 = new Thread(mt, "窗口3");

		// 启动这三个线程,也即是三个窗口,开始卖票
		th1.start();
		th2.start();
		th3.start();
	}
}

2-4 应用揭秘 (02:42)

两种方式的比较

Runnable方式可以避免Thread方式由于Java单继承特性带来的缺陷

Runnable的代码可以被多个线程(Thread实例)共享,适合于多个线程处理同一资源的情况

第3章 线程的生命周期和守护线程

3-1 线程的生命周期 (04:21)

3-2 守护线程理论知识 (02:57)

3-3 守护线程代码示例 (10:23)

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

class DaemonThread implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("进入守护线程" + Thread.currentThread().getName());
		try {
			writeToFile();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("退出守护线程" + Thread.currentThread().getName());
	}

	private void writeToFile() throws Exception {
		// TODO Auto-generated method stub
		File filename = new File("E:" + File.separator + "daemon.txt");
		OutputStream os = new FileOutputStream(filename, true);
		int count = 0;
		while (count < 999) {
			os.write(("
word" + 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());
	}
}

3-4 使用jstack生成线程快照 (11:38)

第4章 课程总结

4-1 课程总结 (02:13)

原文地址:https://www.cnblogs.com/denggelin/p/7163872.html