多线程实现流程

  • 定义一个MyThread类继承Thread类
  • 在MyThread类中重写run()方法
  • 创建MyThread类的对象
  • 启动线程

setName()和getName()

  1. public class MyThreadDemo {
  2. public static void main(String[] args) throws InterruptedException {
  3. MyThread s = new MyThread();
  4. s.setName("倒计时:"); //设置线程名称为倒计时:
  5. s.start();
  6. while (true){
  7. String time = String.valueOf(s.getTime());
  8. if ("0".equals(time)) {
  9. Thread.sleep(1);
  10. System.out.println("时间到");
  11. break;
  12. }
  13. }
  14. }
  15. }
  16. public class MyThread extends Thread{
  17. private int time=-1;
  18. public int getTime() {
  19. return this.time;
  20. }
  21. @Override
  22. public void run() {
  23. for (int i = 5; i >=0 ; i--) {
  24. this.time = i;
  25. System.out.println(getName()+this.time); //获取线程名称
  26. try {
  27. Thread.sleep(1000);
  28. } catch (InterruptedException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }

线程优先级设置

  • setPriority()
  • getPriority() ```java public class MyThreadDemo { public static void main(String[] args) throws InterruptedException {
    1. MyThread s = new MyThread();
    2. s.start();
    3. s.setPriority(2); //设置线程优先级为2 范围为1-10
    4. while (true){
    5. String time = String.valueOf(s.getTime());
    6. if ("0".equals(time)) {
    7. Thread.sleep(1);
    8. System.out.println("时间到");
    9. break;
    10. }
    11. }
    } }

public class MyThread extends Thread{ private int time=-1; public int getTime() { return this.time; } @Override public void run() { for (int i = 5; i >=0 ; i—) { this.time = i; System.out.println(“该线程优先级:”+getPriority()+”\n倒计时”+this.time); //获取线程优先级 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }

  1. <a name="h2sTU"></a>
  2. ## 线程控制
  3. - Thread.sleep()
  4. - *.join()
  5. - *.setDaemon()
  6. ```java
  7. public class MyThread extends Thread{
  8. private int time;
  9. private int i;
  10. public int getTime() {
  11. return this.time;
  12. }
  13. public void seti(int i) {
  14. this.i = i;
  15. }
  16. @Override
  17. public void run() {
  18. for (int i = this.i; i >=0 ; i--) {
  19. this.time = i;
  20. System.out.println("倒计时"+this.time);
  21. try {
  22. Thread.sleep(1000); //设置该线程等待1s后运行
  23. } catch (InterruptedException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  28. }
  29. public class MyThreadDemo {
  30. public static void main(String[] args) throws InterruptedException {
  31. MyThread s1 = new MyThread();
  32. MyThread s2 = new MyThread();
  33. MyThread s3 = new MyThread();
  34. s1.seti(5);
  35. s2.seti(10);
  36. s3.seti(5);
  37. s2.setDaemon(true); //设置s2为守护线程,当只剩下守护线程在运行时,jvm虚拟机会退出
  38. s1.start();
  39. s1.join(); //设置当s1执行完后其他线程才会执行
  40. s2.start();
  41. s3.start();
  42. }
  43. }

通过实现Runnable接口实现多线程

  1. public class MyThread implements Runnable{
  2. private int time;
  3. public void run(){
  4. this.time = 100;
  5. while (true){
  6. if (this.time <= 0){
  7. break;
  8. }
  9. System.out.println(Thread.currentThread().getName()+"正在出售第"+time+"张票");
  10. this.time--;
  11. }
  12. }
  13. }
  14. public class MyThreadDemo {
  15. public static void main(String[] args) throws InterruptedException {
  16. MyThread r = new MyThread();
  17. Thread s1 = new Thread(r,"一号窗口");
  18. Thread s2 = new Thread(r,"二号窗口");
  19. Thread s3 = new Thread(r,"三号窗口");
  20. s1.start();
  21. s2.start();
  22. s3.start();
  23. }
  24. }
  25. //使用该方式开启多线程时使用Thread.currentThread().getName()访问该进程名

同步代码块

  1. public class MyThread implements Runnable{
  2. private int time = 100;
  3. private Object obj = new Object();
  4. public void run(){
  5. while (true){
  6. synchronized (obj){
  7. if (this.time <= 0){
  8. break;
  9. }
  10. try {
  11. Thread.sleep(1000);
  12. } catch (InterruptedException e) {
  13. e.printStackTrace();
  14. }
  15. System.out.println(Thread.currentThread().getName()+"正在出售第"+time+"张票");
  16. this.time--;
  17. }
  18. }
  19. }
  20. }
  21. public class MyThreadDemo {
  22. public static void main(String[] args) throws InterruptedException {
  23. MyThread r = new MyThread();
  24. Thread s1 = new Thread(r,"一号窗口");
  25. Thread s2 = new Thread(r,"二号窗口");
  26. Thread s3 = new Thread(r,"三号窗口");
  27. s1.start();
  28. s2.start();
  29. s3.start();
  30. }
  31. }

同步方法

  1. public class MyThread implements Runnable{
  2. private int time = 100;
  3. public void run(){
  4. while (true){
  5. f();
  6. }
  7. }
  8. private synchronized void f(){
  9. if (this.time <= 0){
  10. System.out.println("出售完毕");
  11. System.exit(0);
  12. }
  13. else {
  14. System.out.println(Thread.currentThread().getName()+this.time);
  15. this.time--;
  16. }
  17. }
  18. }
  19. public class MyThreadDemo {
  20. public static void main(String[] args) throws InterruptedException {
  21. MyThread r = new MyThread();
  22. Thread s1 = new Thread(r,"一号窗口");
  23. Thread s2 = new Thread(r,"二号窗口");
  24. Thread s3 = new Thread(r,"三号窗口");
  25. s1.start();
  26. s2.start();
  27. s3.start();
  28. }
  29. }