Java基础之线程调度与线程创建方式二
调度策略
时间片
抢占式:高优先级的线程抢占CPU
Java的调度方法
同优先级线程组成先进先出队列(先到先服务),使用时间片策略
对高优先级,使用优先调度的抢占式策略
线程的优先级等级
MAX_PRIORITY:10
MIN _PRIORITY:1
NORM_PRIORITY:5
涉及的方法
getPriority() :返回线程优先值
setPriority(int newPriority) :改变线程的优先级
说明
线程创建时继承父线程的优先级
低优先级只是获得调度的概率低,并非一定是在高优先级线程之后才被调用
获取当前线程优先及
Thread.currentThread().getPriority()获取当前线程优先及
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | class My extends Thread{ public My(){} public My(String name){ //构造器 super (name); } @Override public void run() { for ( int i = 0 ;i< 10 ;i++){ if (i % 2 == 0 ){ try { sleep( 1000 ); //休眠一秒 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+i+ ":" +Thread.currentThread().getPriority()); } if (i% 3 == 0 ){ yield(); } } } } public class ThreadMethodTest { public static void main(String[] args) throws InterruptedException { My m1 = new My(); m1.setName( "线程1***" ); //设置线程的名字 m1.start(); Thread.currentThread().setName( "主线程" ); for ( int i = 0 ;i< 10 ;i++){ if (i% 2 == 0 ){ System.out.println(Thread.currentThread().getName()+ ":" +i); } if (i== 2 ){ try { m1.join(); } catch (InterruptedException e){ e.printStackTrace(); } } } System.out.println(m1.isAlive()); //判断m1线程是否存活 } } 测试结果 主线程: 0 主线程: 2 线程 1 *** 0 : 5 线程 1 *** 2 : 5 线程 1 *** 4 : 5 线程 1 *** 6 : 5 线程 1 *** 8 : 5 主线程: 4 主线程: 6 主线程: 8 false Process finished with exit code 0 |
测试
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | class My extends Thread{ public My(){} public My(String name){ //构造器 super (name); } @Override public void run() { System.out.println( "子线程" ); System.out.println(getName()); } } public class ThreadMethodTest { public static void main(String[] args) throws InterruptedException { My m1 = new My(); //设置子线程优先级 m1.setPriority(Thread.MAX_PRIORITY); m1.setName( "线程1***" ); //设置线程的名字 m1.start(); Thread.currentThread().setName( "主线程" ); for ( int i = 0 ;i< 10 ;i++){ if (i% 2 == 0 ){ System.out.println(Thread.currentThread().getName()+ ":" +i); } if (i== 2 ){ try { m1.join(); } catch (InterruptedException e){ e.printStackTrace(); } } } System.out.println(m1.isAlive()); //判断m1线程是否存活 } } 测试 主线程: 0 子线程 主线程: 2 线程 1 *** 主线程: 4 主线程: 6 主线程: 8 false Process finished with exit code 0 |
线程的调度。最后重复10(线程安全为题)
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | class window extends Thread{ private static int tic = 10 ; @Override public void run() { while ( true ){ if (tic> 0 ){ System.out.println(getName()+tic); tic--; } else { break ; } } } } public class windowTest { public static void main(String[] args) { window w1 = new window(); window w2 = new window(); window w3 = new window(); w1.setName( "1---" ); w2.setName( "2---" ); w3.setName( "3----" ); w1.start(); w2.start(); w3.start(); } } 测试结果 3 ---- 10 1 --- 10 2 --- 10 1 --- 8 3 ---- 9 1 --- 6 1 --- 4 1 --- 3 1 --- 2 2 --- 7 1 --- 1 3 ---- 5 Process finished with exit code 0 |
创建实现多线程的方式二实现Runnable 接口、
创建一个实现Runnable接口的类
实现类去实现Runnable中的抽象方法:run()
创建实现类的对象
将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象
通过Thread类的对象去调用start()
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 32 33 34 35 36 | /** * 创建多线程的方式2:实现Runnable方式 * 创建一个实现runnable接口的方法 * 创建实现类的对象 * 将此对象Thread类的对象调用start() * */ //创建一个实现Runnable接口类 class Mthread implements Runnable{ @Override public void run() { for ( int i = 0 ;i< 10 ;i++){ if (i% 2 == 0 ){ System.out.println(i); } } } } public class ThreadTest { public static void main(String[] args) { Mthread mthread= new Mthread(); Thread t1= new Thread(mthread); t1.start(); } } 测试结果 0 2 4 6 8 Process finished with exit code 0 |
售票小窗的实现
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 32 33 34 35 36 37 38 39 40 41 42 | package com.chenxi.java2; class win1 implements Runnable{ private int Tic= 10 ; @Override public void run() { while ( true ){ if (Tic> 0 ){ System.out.println(Thread.currentThread().getName()+ ":" +Tic); Tic--; } else { break ; } } } } public class WindowTest1 { public static void main(String[] args) { win1 w1= new win1(); Thread t1 = new Thread(w1); Thread t3 = new Thread(w1); Thread t2 = new Thread(w1); t1.start(); t2.start(); t3.start(); } } 测试结果 Thread- 0 : 10 Thread- 1 : 10 Thread- 2 : 10 Thread- 2 : 7 Thread- 2 : 6 Thread- 2 : 5 Thread- 2 : 4 Thread- 1 : 8 Thread- 1 : 2 Thread- 1 : 1 Thread- 0 : 9 Thread- 2 : 3 Process finished with exit code 0 |
开发中优先选择实现Runnable接口的方式
1.天然适合处理共享数据
2.没有类的单继承局限
发现Thread 基础Runnable方式
相同 两种方式都需要Run(),将线程的方式run方法里
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
2020-04-16 MongoDB的安装及简单使用
2020-04-16 Mysql 读写分离