三种

  1. Thread class :继承Thread类
  2. Runnable 接口:实现Runnable接口、
  3. Callable接口:实现Callable接口

Thread

  • 自定义线程类继承Thread
  • 重写run()方法,编写线程执行体
  • 创建线程对象,调用start()方法启动线程
  1. package com.kai.lesson1;
  2. public class TestThread1 extends Thread{
  3. @Override
  4. public void run() {
  5. for (int i=0;i<20;i++){
  6. System.out.println("rum方法");
  7. }
  8. }
  9. public static void main(String[] args) {
  10. //main线程,主线程
  11. TestThread1 testThread1=new TestThread1();
  12. testThread1.start();
  13. for (int i=0;i<20000;i++){
  14. System.out.println("主线程");
  15. }
  16. }
  17. }
  1. 线程开启不一定立即执行,由CPU安排调度

例子

  1. package com.kai.lesson1;
  2. //联系Thread,实现多线程同步下载图片
  3. import org.apache.commons.io.FileUtils;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. public class TestThread2 extends Thread{
  9. private String url;
  10. private String name;
  11. public TestThread2(String url, String name){
  12. this.url=url;
  13. this.name=name;
  14. }
  15. @Override
  16. public void run() {
  17. WebDownloader webDownloader=new WebDownloader();
  18. try {
  19. webDownloader.dowanloader(url,name);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. System.out.println("下载了文件名为:"+name);
  24. }
  25. public static void main(String[] args) {
  26. TestThread2 t1=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","2.jpg");
  27. TestThread2 t2=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","3.jpg");
  28. TestThread2 t3=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","4.jpg");
  29. t1.start();
  30. t2.start();
  31. t3.start();
  32. }
  33. }
  34. //下载器
  35. class WebDownloader{
  36. //下载方法
  37. public void dowanloader(String url, String name) throws IOException {
  38. try {
  39. FileUtils.copyURLToFile(new URL(url),new File(name));
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. System.out.println("IOy异常,downloader方法出现问题");
  43. }
  44. }
  45. }

Runnable

创建线程方式:

  1. 实现runnable接口
  2. 重写run方法
  3. 执行线程需要丢入runnable接口实现类,调用start方法
  1. package com.kai.lesson1;
  2. public class TestThread3 implements Runnable {
  3. @Override
  4. public void run() {
  5. for (int i=0;i<20;i++){
  6. System.out.println("rum方法");
  7. }
  8. }
  9. public static void main(String[] args) {
  10. //创建runnable接口实现类对象
  11. TestThread3 testThread3=new TestThread3();
  12. //创建线程对象,通过线程对象开启线程,代理
  13. //Thread thread=new Thread(testThread3);
  14. //thread.start();
  15. new Thread(testThread3).start();
  16. for (int i=0;i<20000;i++){
  17. System.out.println("主线程");
  18. }
  19. }
  20. }

例子

  1. package com.kai.lesson1;
  2. //联系Thread,实现多线程同步下载图片
  3. import org.apache.commons.io.FileUtils;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. public class TestThread2 implements Runnable{
  9. private String url;
  10. private String name;
  11. public TestThread2(String url, String name){
  12. this.url=url;
  13. this.name=name;
  14. }
  15. @Override
  16. public void run() {
  17. WebDownloader webDownloader=new WebDownloader();
  18. try {
  19. webDownloader.dowanloader(url,name);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. System.out.println("下载了文件名为:"+name);
  24. }
  25. public static void main(String[] args) {
  26. TestThread2 t1=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","22.jpg");
  27. TestThread2 t2=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","33.jpg");
  28. TestThread2 t3=new TestThread2("http://i0.hdslb.com/bfs/bangumi/image/86fdce81522f76b8b323776c7c77f044e13fc808.png","44.jpg");
  29. /*
  30. t1.start();
  31. t2.start();
  32. t3.start();
  33. */
  34. new Thread(t1).start();
  35. new Thread(t2).start();
  36. new Thread(t3).start();
  37. }
  38. }
  39. //下载器
  40. class WebDownloader{
  41. //下载方法
  42. public void dowanloader(String url, String name) throws IOException {
  43. try {
  44. FileUtils.copyURLToFile(new URL(url),new File(name));
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. System.out.println("IOy异常,downloader方法出现问题");
  48. }
  49. }
  50. }

总结

image.png