麦麦脆汁鸡

导航

线程创建

image-20220315195835654

 

继承Thread类

package com.thread.demo01;

//创建线程方式一:继承Thread类,重写run()方法,调用start方法开启线程

//总结:注意,线程开启不一定立即执行,由CPU调度执行

public class TestThread1 extends Thread{
   @Override
   public void run() {
       //run方法 分线程的线程体
       for (int i = 0; i < 20; i++) {
           System.out.println("我在看动漫"+i);
      }
  }

   public static void main(String[] args) {
       //main方法 主线程的线程体

       //创建一个线程对象,调用start方法开启线程
       TestThread1 testThread1 = new TestThread1();
       testThread1.start();

       for (int i = 0; i < 20; i++) {
           System.out.println("我在学习"+i);
      }
  }
}

 

实现Runnable接口

package com.thread.demo01;

//创建线程方式二:实现runnable接口,重写run()方法,执行线程需要丢入runnable接口的实现类,调用start方法

public class TestThread2 implements Runnable{
   @Override
   public void run() {
       //run方法 分线程的线程体
       for (int i = 0; i < 20; i++) {
           System.out.println("我在看动漫"+i);
      }
  }

   public static void main(String[] args) {
       //创建runnable接口的实现类对象
       TestThread2 testThread2 = new TestThread2();

       //创建线程对象,通过线程对象来开启我们的线程,代理
       Thread thread = new Thread(testThread2);

       thread.start();//可以和上一行合并为:new Thread(testThread2).start();

       for (int i = 0; i < 20; i++) {
           System.out.println("我在学习"+i);
      }
  }
}
  • 推荐使用Runnable接口,避免单继承局限性,灵活方便,方便同一个对象被多个线程使用。

 

初识并发问题
package com.thread.demo01;

//多个线程同时操作同一个对象
//买火车票的例子
public class TestThread4 implements Runnable{

   //票数
   private int ticketNums = 10;

   @Override
   public void run() {
       while(true){
           if(ticketNums<=0){
               break;
          }
           //模拟延时
           try {
               Thread.sleep(200);
          } catch (InterruptedException e) {
               e.printStackTrace();
          }

           System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNums--+"张票");
      }
  }

   public static void main(String[] args) {
       TestThread4 ticket = new TestThread4();

       new Thread(ticket,"小明").start();
       new Thread(ticket,"老师").start();
       new Thread(ticket,"黄牛").start();
  }
}

 

案例:龟兔赛跑
package com.thread.demo01;

//模拟龟兔赛跑
public class Race implements Runnable{

   private static String winner;//胜利者

   @Override
   public void run() {
       for (int i = 0; i <= 100; i++) {

           //模拟兔子休息
           if(Thread.currentThread().getName().equals("兔子")&&i%10==0){
               try {
                   Thread.sleep(1);
              } catch (InterruptedException e) {
                   e.printStackTrace();
              }
          }

           //调用判断比赛是否结束方法
           boolean flag = gameOver(i);
           if(flag){
               break;
          }//如果比赛结束了,停止程序

           System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
      }
  }

   //判断比赛是否结束 方法
   private boolean gameOver(int steps){
       if(winner!=null){
           return true;
      }{
           if(steps==100){
               winner = Thread.currentThread().getName();
               System.out.println("胜利者是"+winner);
               return true;
          }
      }
       return false;

  }

   public static void main(String[] args) {
       Race race = new Race();

       new Thread(race,"兔子").start();
       new Thread(race,"乌龟").start();
  }
}

 

posted on 2022-03-16 17:50  麦麦脆汁鸡  阅读(105)  评论(0编辑  收藏  举报