每一個進程執行都有一個執行路徑,或者叫控制單元。
2、線程:進程中一個獨立的控制單元。
線程在控制着進程的執行。一個進程中至少有一個線程
擴展:其實JVM本身就是一個多線程,主線程負責邏輯運算,
還有一個負責垃圾回收。
線程中爲什麼要重寫run()方法?
Thread類用於描述線程,該類就定義了一個功能,用於存儲
線程要執行的代碼,run()方法就是存儲功能。
public class ThredDemo {
private static int[] datas={15,20,23,12,10};
public static void main(String[] args) {
CustomThread thread=new CustomThread();
/**
* 區別:
* start()表明啓動了子線程,排序方法在子線程中運行
* run()沒有啓動子線程,就是普通的調用,排序方法仍然在主線程中執行
*/
thread.start();
thread.run();
}
private static class CustomThread extends Thread
{
public void run()
{
int temp=0;
for(int i=0;i<datas.length;i++)
{
for(int j=datas.length-1;j>i;j--)
{
if(datas[j]>datas[j-1])
{
temp=datas[j];
datas[j]=datas[j-1];
datas[j-1]=temp;
}
}
}
System.out.println("排序後:");
for(int i=0;i<datas.length;i++)
{
System.out.print(datas[i]+" ");
}
}
}
}