线程的五种状态
线程的常见方法
1.start():启动当前线程,表面上调用start()方法,实际在调用线程里面的run方法
2.run():线程类 继承Thread类 或者 实现Runnable接口的时候,都要重新实现这个run方法,run方法里面是线程要执行的内容
3.currentThread:Thread类中一个静态方法;获取当前正在执行的线程
4.setName 设置线程名字
5.getName 读取线程名字
线程的优先级别
1.同优先级别的线程,采取的策略就是先到先服务,使用时间片策略
2.如果优先级别高,被CPU调度的概率就高
3.级别:通过Thread类的源码知道,1-10 默认的级别为5
源码:
public final static int MIN_PRIORITY=1;
public final static int NORM_PRIORITY=5;
public final static int MAX_PRIORITY=10;
public class MyThread extends Thread{
@Override
public void run() {
for (int i=1;i<=10;i++){
System.out.println(i);
}
}
}
class MyThread02 extends Thread{
@Override
public void run() {
for (int i=20;i<=30;i++){
System.out.println(i);
}
}
}
class TestThread{
//这是main方法
public static void main(String[] args) {
//创建两个子线程,让这两个子线程争抢资源
MyThread myThread = new MyThread();
myThread.setPriority(10);//设置优先级高
myThread.start();
MyThread02 myThread02 = new MyThread02();
myThread02.setPriority(1);//优先级别低
myThread02.start();
}
}public class MyThread extends Thread{
@Override
public void run() {
for (int i=1;i<=10;i++){
System.out.println(i);
}
}
}
class MyThread02 extends Thread{
@Override
public void run() {
for (int i=20;i<=30;i++){
System.out.println(i);
}
}
}
class TestThread{
//这是main方法
public static void main(String[] args) {
//创建两个子线程,让这两个子线程争抢资源
MyThread myThread = new MyThread();
myThread.setPriority(10);//设置优先级高
myThread.start();
MyThread02 myThread02 = new MyThread02();
myThread02.setPriority(1);//优先级别低
myThread02.start();
}
}public class MyThread extends Thread{
@Override
public void run() {
for (int i=1;i<=10;i++){
System.out.println(i);
}
}
}
class MyThread02 extends Thread{
@Override
public void run() {
for (int i=20;i<=30;i++){
System.out.println(i);
}
}
}
class TestThread{
//这是main方法
public static void main(String[] args) {
//创建两个子线程,让这两个子线程争抢资源
MyThread myThread = new MyThread();
myThread.setPriority(10);//设置优先级高
myThread.start();
MyThread02 myThread02 = new MyThread02();
myThread02.setPriority(1);//优先级别低
myThread02.start();
}
}public class MyThread extends Thread{
@Override
public void run() {
for (int i=1;i<=10;i++){
System.out.println(i);
}
}
}
class MyThread02 extends Thread{
@Override
public void run() {
for (int i=20;i<=30;i++){
System.out.println(i);
}
}
}
class TestThread{
//这是main方法
public static void main(String[] args) {
//创建两个子线程,让这两个子线程争抢资源
MyThread myThread = new MyThread();
myThread.setPriority(10);//设置优先级高
myThread.start();
MyThread02 myThread02 = new MyThread02();
myThread02.setPriority(1);//优先级别低
myThread02.start();
}
}