调度策略
时间片
抢占式:高优先级的线程抢占CPU
Java的调度方法
同优先级线程组成先进先出队列(先到先服务),使用时间片策略
对高优先级,使用优先调度的抢占式策略
线程的优先级等级
MAX_PRIORITY:10
MIN _PRIORITY:1
NORM_PRIORITY:5
涉及的方法
getPriority() :返回线程优先值
setPriority(int newPriority) :改变线程的优先级
说明
线程创建时继承父线程的优先级
低优先级只是获得调度的概率低,并非一定是在高优先级线程之后才被调用
获取当前线程优先及
Thread.currentThread().getPriority()获取当前线程优先及
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
测试
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(线程安全为题)
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()
/**
* 创建多线程的方式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
售票小窗的实现
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方法里