分配时间段,每个线程运行一个时间段换另一个时间段,从整体看好像都在运行,其实同一时间只在运行一个线程
调度规则:
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);
}
}
}