线程和进程的区别 线程是在进程内部做的事情。
线程的调用方式 继承Thread类 hero.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package hero;public class Hero { public String name; public float hp; public int damage; public void attackHero (Hero h) { try { Thread.sleep(1000 ); h.hp = h.hp-this .damage; System.out.println(this .name+"攻击了" +h.name+"造成了" +this .damage+"点伤害" ); }catch (InterruptedException ie){ ie.printStackTrace(); } if (h.isDead()){ System.out.println(h.name+"isDead!" ); } } public boolean isDead () { return hp<=0 ; } }
ExtendsThreadKill.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package ThreadTest1;import hero.Hero;public class ExtendsThreadKill extends Thread { private Hero h1; private Hero h2; ExtendsThreadKill(Hero h1, Hero h2){ this .h1=h1; this .h2=h2; } public void run () { while (!h2.isDead()){ h1.attackHero(h2); } } }
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import hero.Hero;import java.util.ArrayList;import java.util.List;import java.util.Random;public class ThreadTest { public List<Hero> GenerateHero () { String[] name = new String[]{"盖伦" ,"盲僧" ,"提莫" ,"赏金" }; List<Hero> heros = new ArrayList<>(); for (int i=0 ;i<4 ;i++){ Hero hero = new Hero(); hero.name = name[i]; hero.hp = new Random().nextFloat()*20 ; System.out.println(hero.name+"诞生,血量为" +hero.hp); hero.damage = new Random().nextInt(7 ); heros.add(hero); } return heros; } public static void main (String[] args) { List<Hero> hero = new ThreadTest().GenerateHero(); ExtendsThreadKill th1 = new ExtendsThreadKill(hero.get(1 ),hero.get(0 )); ExtendsThreadKill th2 = new ExtendsThreadKill(hero.get(3 ),hero.get(2 )); th1.start(); th2.start(); } }
实现Runnable接口 OverwriteRunnable.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package ThreadTest2;import hero.Hero;public class OverwriteRunnable implements Runnable { private Hero h1; private Hero h2; public OverwriteRunnable (Hero h1, Hero h2) { this .h1=h1; this .h2=h2; } @Override public void run () { while (!h2.isDead()){ h1.attackHero(h2); } } }
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package ThreadTest2;import ThreadTest1.ThreadTest;import hero.Hero;import java.util.List;public class TestOverwriteRunnable { public static void main (String[] args) { List<Hero> h = new ThreadTest().GenerateHero(); OverwriteRunnable o1 = new OverwriteRunnable(h.get(0 ),h.get(1 )); OverwriteRunnable o2 = new OverwriteRunnable(h.get(2 ),h.get(3 )); new Thread(o1).start(); new Thread(o2).start(); } }
匿名类 在java8中,匿名类可以改写成lamda表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package ThreadTest3;import ThreadTest1.ThreadTest;import hero.Hero;import java.util.List;public class TestThread3 { public static void main (String[] args) { List<Hero> hs = new ThreadTest().GenerateHero(); Thread t1 = new Thread(() -> { while (!hs.get(1 ).isDead()) { hs.get(2 ).attackHero(hs.get(1 )); } }); Thread t2 = new Thread(() -> { while (!hs.get(0 ).isDead()) { hs.get(3 ).attackHero(hs.get(0 )); } }); t1.start(); t2.start(); } }
练习 遍历文件夹,若找到txt文件,则启动一个线程,将内容输出到控制台 FileReadThread.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package practice;import java.io.*;public class FileReadThread extends Thread { private File f; FileReadThread(File file) { this .f = file; } public void run () { System.out.println(FindContent(f)); } private String FindContent (File f) { try (FileReader fr = new FileReader(f)) { char [] content= new char [(int )f.length()]; fr.read(content); fr.close(); return new String(content); } catch (IOException e){ e.printStackTrace(); return null ; } } }
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package practice;import java.io.File;public class TestFile { private void Search (File f) { if (f!=null ){ if (f.isFile()){ if (f.getName().endsWith(".txt" )){ new FileReadThread(f).start(); } }else if (f.isDirectory()){ File[] files = f.listFiles(); assert files != null ; for (File file:files){ Search(file); } } } } public static void main (String[] args) { File f = new File("C:\\Users\\Jacob\\OneDrive\\Calibre" ); TestFile t = new TestFile(); t.Search(f); } }