1. 设计两个线程,一个线程负责打印1100以内所有的偶数;然后,另外一个线程负责打印1100以内所有的奇数。
测试时,分别设置线程的优先级,观察执行的顺序。
package com.blb.thread.homework;
class Thread01 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 ==0){
System.out.println("线程1打印偶数"+"-----" + i);
}
}
}
}
class Thread02 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 !=0){
System.out.println("线程2打印奇数"+"-----" + i);
}
}
}
}
public class Demo01 {
public static void main(String[] args) {
Thread thread01 = new Thread();
thread01.setPriority(1);
Thread02 thread02 = new Thread02();
thread02.setPriority(10);
thread01.start();
thread02.start();
}
}
2.实现一个线程,用于扫描某个目录下的所有文本文件(包括:java、txt、html),并将文字内容打印出来。
package com.blb.thread.homework;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Demo02Thread extends Thread{
//定义一个递归操作的方法
public static void diGui(File f, List<File> dirList, List<File> fileList){
//1. 判断,如果是文件,则直接存放到集合当中
if (f.isFile()){
fileList.add(f);
}
//2. 判断,如果是文件夹,则直接存放到文件夹的集合。还需要得到子文件数组,遍历递归
if (f.isDirectory()){
dirList.add(f);
//------------------
//获取到亲儿子 子文件的对象数组
File[] array = f.listFiles();
//判断当前的数组是否不为空,防止空指针异常的问题
if (array!=null){
//增强for,获取到每一个儿子
for (File child : array) {
//再次获取文件和文件夹,递归操作
diGui(child,dirList,fileList);
}
}
//------------------
}
}
@Override
public void run() {
//准备数据
File f = new File("C:\\Users\\LENOVO\\Desktop\\demo\\thread\\src\\com\\blb\\thread\\dir");
List<File> dirList = new ArrayList<>();
List<File> fileList = new ArrayList<>();
//直接调用方法
Demo02Thread.diGui(f,dirList,fileList);
//查看结果
System.out.println("文件夹的数据如下:");
//采用lambda表达式快速遍历集合。里面使用方法引用
dirList.forEach(System.out::println);
System.out.println("--------------");
System.out.println("文件的数据如下:");
//采用lambda表达式快速遍历集合。里面使用方法引用
// fileList.forEach(System.out::println);
for (File file : fileList) {
String fileName = file.toString();
if (fileName.endsWith(".java")||fileName.endsWith(".txt")||fileName.endsWith(".html")){
System.out.println(fileName);
//创建文件输入流
try (FileInputStream in = new FileInputStream(fileName)){
//创建字节数组
byte[] buffer = new byte[300];
//每次读取的长度
int length = 0;
//循环将数据读取到字节数组中,到末尾位置
while((length = in.read(buffer)) != -1){
//将字节数组转换为字符串
String str = new String(buffer,0,length,"utf-8");
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class Demo02 {
public static void main(String[] args) {
Demo02Thread demo02Thread = new Demo02Thread();
demo02Thread.start();
}
}
3.某人正在看电视连续剧,从第1~88集,看到第10集时,来了一个送快递的,收完快递后后,继续看电视。
// 看电视线程
class WatchTvThread extends Thread{
@Override
public void run() {
for (int i = 1; i < 88; i++) {
System.out.println("看电视剧看到第"+ i +"集了!");
if (i == 10){
GetProThread getProThread = new GetProThread();
try {
getProThread.start();
getProThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// 收快递线程
class GetProThread extends Thread{
@Override
public void run() {
System.out.println("开门取快递了呀!");
}
}
public class Demo03 {
public static void main(String[] args) {
WatchTvThread watchTvThread = new WatchTvThread();
watchTvThread.start();
}
}
4.2. 多线程模拟龟兔赛跑:
乌龟和兔子进行1000米赛跑,兔子前进5米,乌龟只能前进1米。
但兔子每20米要休息500毫秒,而乌龟是每100米休息500毫秒。
谁先到终点就结束程序,并显示获胜方
package com.blb.thread.homework;
class TuThread extends Thread{
@Override
public void run() {
for (int i = 0; i <= 1000; i=i+5) {
System.out.println("兔子跑了"+i);
if (i % 20 == 0){
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i >= 1000){
System.out.println("兔子胜利了");
}
}
}
}
}
class GuiThread extends Thread{
@Override
public void run() {
for (int i = 0; i <= 1000; i++) {
System.out.println("乌龟跑了"+i);
if (i % 100 == 0){
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i >= 1000){
System.out.println("乌龟胜利了");
}
}
}
}
}
public class Demo04 {
public static void main(String[] args) {
TuThread tuThread = new TuThread();
GuiThread guiThread = new GuiThread();
tuThread.start();
guiThread.start();
}
}