麦麦脆汁鸡

导航

< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5

统计

线程创建

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   麦麦脆汁鸡  阅读(108)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示