CPU是如何处理多线程的运行,及调度规则

分配时间段,每个线程运行一个时间段换另一个时间段,从整体看好像都在运行,其实同一时间只在运行一个线程

调度规则:
1平均分配
2抢占式(按优先级)
设置优先级setPriority

==主方法代码==
public class Ztest {
public static void main(String[] args) {
	MyThread thread=new MyThread();
	MyThread1 thread1=new MyThread1();
//	Thread mainthread=Thread.currentThread();
//	System.out.println(mainthread.MIN_PRIORITY);
//	System.out.println(mainthread.MAX_PRIORITY);
//	System.out.println(mainthread.NORM_PRIORITY);
//	System.out.println(thread.MAX_PRIORITY);
	thread.start();//启动线程
	thread1.start();//启动线程
	thread.setPriority(10);
	thread1.setPriority(1);
//	mainthread.setPriority(1);
//	for(int i=0;i<1000;i++) {
//		System.out.println("mainThread:"+i);
//	}
}
}
==第一个线程:==
import java.util.Scanner;

public class MyThread extends Thread {
	@Override
	public void run() {
		for(int i=0;i<1000;i++) {
			System.out.println("MyThread:"+i);
		}
	} 
}
==第一个线程:==
	public class MyThread1 extends Thread {
		@Override
		public void run() {
			for(int j=0;j<1000;j++) {
				System.out.println("MyThread1:"+j);
			}
		} 
	}	
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值