学习笔记线程的创建方式

刺骨的言语ヽ痛彻心扉 2021-11-04 14:34 322阅读 0赞

学习笔记

    • 5、线程池

多线程创建方式

1.继承Thread类,重写run方法

  1. public class a extends Thread{
  2. public void run() {
  3. while(true) {
  4. try {
  5. sleep(50);
  6. } catch (InterruptedException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. System.out.println("旭旭520");
  11. }
  12. }`
  13. public static void main(String[] args) {
  14. new a().start();
  15. new b().start();
  16. new c().start();
  17. }
  18. }
  19. class b extends Thread{
  20. public void run() {
  21. while(true) {
  22. try {
  23. sleep(50);
  24. } catch (InterruptedException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }
  28. System.out.println("xx520");
  29. }
  30. }
  31. }
  32. class c extends Thread{
  33. public void run() {
  34. while(true) {
  35. try {
  36. sleep(50);
  37. } catch (InterruptedException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. System.out.println("zz520");
  42. }
  43. }
  44. }

执行结果:
旭旭520
zz520
xx520
旭旭520
zz520
xx520
旭旭520
zz520
xx520
旭旭520
xx520
zz520
旭旭520
xx520
zz520
旭旭520
xx520
zz520

2.实现Runnable接口,重写run()方法,将类的实例作为Thread的参数

  1. public class a implements Runnable {
  2. public void run() {
  3. while (true) {
  4. try {
  5. Thread.sleep(50);
  6. } catch (InterruptedException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. System.out.println("旭旭520");
  11. }
  12. }
  13. public static void main(String[] args) {
  14. new Thread(new a()).start();
  15. new Thread(new b()).start();
  16. new Thread(new c()).start();
  17. }
  18. }
  19. class b implements Runnable {
  20. public void run() {
  21. while (true) {
  22. try {
  23. Thread.sleep(50);
  24. } catch (InterruptedException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }
  28. System.out.println("xx520");
  29. }
  30. }
  31. }
  32. class c implements Runnable {
  33. public void run() {
  34. while (true) {
  35. try {
  36. Thread.sleep(50);
  37. } catch (InterruptedException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. System.out.println("zz520");
  42. }
  43. }
  44. }

执行结果:
旭旭520
zz520
xx520
旭旭520
zz520
旭旭520
xx520
zz520
旭旭520
xx520
zz520
xx520
旭旭520

3.实现Callable接口,重写call方法将实现了Callable的类的一个实例作为FutureTask的参数,再将一个FutureTask的实例作为Thread的参数

  1. import java.util.concurrent.Callable;
  2. import java.util.concurrent.FutureTask;
  3. public class a implements Callable<String> {
  4. public String call() {
  5. while (true) {
  6. try {
  7. Thread.sleep(50);
  8. } catch (InterruptedException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. System.out.println("zz520");
  13. }
  14. }
  15. public static void main(String[] args) {
  16. Callable<String> c1 = new a();
  17. FutureTask<String> f1 = new FutureTask<String>(c1);
  18. new Thread(f1).start();
  19. Callable<String> c2 = new b();
  20. FutureTask<String> f2 = new FutureTask<String>(c2);
  21. new Thread(f2).start();
  22. Callable<String> c3 = new c();
  23. FutureTask<String> f3 = new FutureTask<String>(c3);
  24. new Thread(f3).start();
  25. }
  26. }
  27. class b implements Callable<String> {
  28. public String call() {
  29. while (true) {
  30. try {
  31. Thread.sleep(50);
  32. } catch (InterruptedException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. System.out.println("旭旭520");
  37. }
  38. }
  39. }
  40. class c implements Callable<String> {
  41. public String call() {
  42. while (true) {
  43. try {
  44. Thread.sleep(50);
  45. } catch (InterruptedException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. System.out.println("xx520");
  50. }
  51. }
  52. }

执行结果:
旭旭520
xx520
zz520
旭旭520
xx520
旭旭520
zz520
xx520
zz520
旭旭520
xx520

4.使用线程池Executors类,使用ExecutorService e = Executors.newFixedThreadPool(5);再使用e.submit方法将实现了Runable接口的实例执行线程

  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3. public class a implements Runnable {
  4. public void run() {
  5. while (true) {
  6. try {
  7. Thread.sleep(50);
  8. } catch (InterruptedException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. System.out.println("xx520");
  13. }
  14. }
  15. public static void main(String[] args) {
  16. ExecutorService e = Executors.newFixedThreadPool(5);
  17. e.submit(new a());
  18. e.submit(new b());
  19. e.submit(new c());
  20. }
  21. }
  22. class b implements Runnable {
  23. public void run() {
  24. while (true) {
  25. try {
  26. Thread.sleep(50);
  27. } catch (InterruptedException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. System.out.println("旭旭520");
  32. }
  33. }
  34. }
  35. class c implements Runnable {
  36. public void run() {
  37. while (true) {
  38. try {
  39. Thread.sleep(50);
  40. } catch (InterruptedException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. System.out.println("zz520");
  45. }
  46. }
  47. }

执行结果:
zz520
xx520
旭旭520
zz520
xx520
旭旭520
xx520
zz520
zz520
xx520
旭旭520

5、线程池

  1. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 10, 1000, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
  2. threadPoolExecutor.execute(new AlertBug());

发表评论

表情:
评论列表 (有 0 条评论,322人围观)

还没有评论,来说两句吧...

相关阅读

    相关 创建线方式

    方式1:继承Java.lang.Thread类,并覆盖run() 方法。优势:编写简单;劣势:无法继承其它父类 public class ThreadDemo1 \{ pu