線程對象通過start方法進(jìn)入runnable狀態(tài),啟動的線程不一定會立即得到執(zhí)行,線程的運(yùn)行與否要看cpu的調(diào)度,我們把這個(gè)中間狀態(tài)叫可執(zhí)行狀態(tài)(RUNNABLE)。
static關(guān)鍵字詳解
public class Person {
//2:賦初始值~
{
System.out.println("匿名代碼塊");
}
//1: 只執(zhí)行一次~
static {
System.out.println("靜態(tài)代碼塊");
}
//3
public Person() {
System.out.println("構(gòu)造方法");
}
public static void main(String[] args) {
Person p1 = new Person();
System.out.println("================");
Person p2 = new Person();
}
}
//輸出結(jié)果
靜態(tài)代碼塊
匿名代碼塊
構(gòu)造方法
================
匿名代碼塊
構(gòu)造方法
Process finished with exit code 0
//static
public class Student {
private static int age;//靜態(tài)的變量 多線程!
private double score; //非靜態(tài)的變量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
new Student().run();
Student.go();
go();
}
}
//靜態(tài)導(dǎo)入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test1 {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
每日J(rèn)ava面試題
1.線程N(yùn)EW狀態(tài)
new創(chuàng)建一個(gè)Thread對象時(shí),并沒處于執(zhí)行狀態(tài),因?yàn)闆]有調(diào)用start方法啟動改線程,那么此時(shí)的狀態(tài)就是新建狀態(tài)。
2.線程RUNNABLE狀態(tài)
線程對象通過start方法進(jìn)入runnable狀態(tài),啟動的線程不一定會立即得到執(zhí)行,線程的運(yùn)行與否要看cpu的調(diào)度,我們把這個(gè)中間狀態(tài)叫可執(zhí)行狀態(tài)(RUNNABLE)。
3.線程的RUNNING狀態(tài)
一旦cpu通過輪詢或其他方式從任務(wù)可以執(zhí)行隊(duì)列中選中了線程,此時(shí)它才能真正的執(zhí)行自己的邏輯代碼。