/**
* 了解,线程的调度:
* 1、常见的线程调度模型有哪些?
* 抢占式调度模型:
* 哪个线程的优先级比较高,抢到的CPU时间片的概率就高一些/多一些
* java采用的就是抢占式调度模型
* 2、均分是调度模型:
* 平均分配CPU时间片,每个线程占有的CPU时间片时间长度一样
* 平均分配,一切平等,一些编程语言,线程调度模型采用的是这种模型
*
* 1、void setPriority(int newPriority) 设置线程的优先级
* int getPriority() 返回线程的优先级
* 最低优先级1
* 默认优先级5
* 最高优先级10
* 优先级高的获取CPU时间片可能会多一些,概率是高的
*
*
* static void yield() 静态方法,暂停当前正在执行的线程对象,执行其他线程
* yield()方法不是阻塞方法,让当前线程让位,让给其他线程使用
* yield()方法的执行会让当前线程从“运行状态”回到“就绪状态”(回到就绪后,可能还会抢到,只是概率较低)
*
* void join() 合并线程
* class MyThread extends Thread{
* public void doSome(){
* MyThread02 aa = new MyThread02();
* aa.join()
* //当前线程阻塞,aa线程执行,知道aa线程结束,当前线程才可以继续
* }
* }
* class MyThread02 extends Thread{
*
* }
*
*/
public class Note02 {
public Note02() {
}
}
public class ThreadTest08 {
public static void main(String[] args) {
System.out.println("最高优先级:" + Thread.MAX_PRIORITY);
System.out.println("最低优先级:" + Thread.MIN_PRIORITY);
System.out.println("默认优先级:" + Thread.NORM_PRIORITY);
Thread aa = Thread.currentThread();
System.out.println(aa.getName() + "线程的默认优先级为:" + aa.getPriority());
System.out.println("---------------------------");
Thread.currentThread().setPriority(1);
Thread bb = new My07();
bb.setPriority(10);
bb.start();
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
class My07 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
public class ThreadTest09 {
public static void main(String[] args) {
Thread aa = new My08();
aa.start();
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
class My08 extends Thread{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
if(i % 5 ==0){
Thread.yield();
}
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
public class ThreadTest10 {
public static void main(String[] args) {
Thread aa = new My09();
aa.start();
try {
aa.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("asdgfdgt");
}
}
class My09 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "---->" + i);
}
}
}