1.概念理解java
進程:是操做系統結構的基礎;是一次程序的執行;是一個程序及其數據在處理機上順序執行時所發生的活動;是程序在一個數據
集合上運行的過程,它是系統進行資源分配和調度的一個獨立單位。
線程:是在進程中獨立運行的子任務。
多線程的優勢:能夠最大限度的利用cpu的空閒時間來處理其餘任務。安全
線程實現:多線程
線程能夠經過集成Thread :可是java不支持多繼承。
實現Runable接口ide
注意點:執行start()方法的順序不表明線程啓動的順序。this
2:對比數據的共享spa
public class ConThread extends Thread{
public int count=5;
public String name;
public ConThread(String name){
this.name=name;
this.setName(name);
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
if(count>0){
count--;
System.out.println("this Thread name is :"+this.currentThread().getName()+"--"+count);
}
}
}
ConThread th1=new ConThread("A");
ConThread th2=new ConThread("B");}}
ConThread th3=new ConThread("C");
ConThread th4=new ConThread("D");
th1.start();
th2.start();
th3.start();
th4.start()
以上的線程經過newThread之間建立start,數據count並不會共享,每一個線程都會輸出4,3,2,1,操作系統
public class ConThread extends Thread{
public int count=5;
public String name;
public ConThread(){
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
// while(count>0){
count--;
System.out.println("this Thread name is :"+this.currentThread().getName()+"--"+count);
//}
}
}
ConThread th=new ConThread();
Thread th0=new Thread(th,"A");
Thread th1=new Thread(th,"B");
Thread th2=new Thread(th,"C");
th0.start();
th1.start();
th2.start();
以上線程則是傳入構造方法,建立Thread,則會共享數據count,可是線程非安全。線程
3: synchronized 關鍵字對象
public class ConThread extends Thread{
public int count=5;
public String name;
public ConThread(){
}
@Override
synchronized public void run() {
// TODO Auto-generated method stub
super.run();
// while(count>0){
count--;
System.out.println("this Thread name is :"+this.currentThread().getName()+"--"+count);
//}
}
}
ConThread th=new ConThread();
Thread th0=new Thread(th,"A");
Thread th1=new Thread(th,"B");
Thread th2=new Thread(th,"C");
th0.start();
th1.start();
th2.start();
4:currentThread blog
isAlive 判斷線程是否存活狀態,start 後
public static void main(String[] args) {
// TODO Auto-generated method stub
CountOperate c=new CountOperate();
Thread th0=new Thread(c,"A");
Thread th1=new Thread(c,"B");
// System.out.println("th0 id :"+th0.getId());
//System.out.println("main begain th0 is alive :"+th0.isAlive());
// th0.setName("A");
th0.start();
th1.start();
// System.out.println("th0 id :"+th0.getId());
// System.out.println("main begain th0 is alive :"+th0.isAlive());
public class CountOperate extends Thread {
public CountOperate() {
System.out.println("countOperate --begain");
System.out.println("Thread.currentThread.getName():"+Thread.currentThread().getName());
System.out.println("Thread.currentThread.isAlive:"+Thread.currentThread().isAlive());
System.out.println("this.isAlive:"+this.isAlive());
System.out.println("this.getName:"+this.getName());
System.out.println("countOperate --end");
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("cunThread.getId:"+this.getId());
System.out.println("run --begain");
System.out.println("Thread.currentThread.getName():"+Thread.currentThread().getName()); // currentThread 是執行該代碼塊的線程
//System.out.println("Thread.currentThread.isAlive:"+Thread.currentThread().isAlive());
//System.out.println("this.isAlive:"+this.isAlive()); // 是false 搞不懂
//System.out.println("cunThread.getId:"+this.getId());
// this指當前的線程 CountOperate 該對象傳入new Thread ,run 是new Thread 對象調用的 便是 由 A,B 調用
System.out.println("this.getName:"+this.getName());
System.out.println("run --end");
}
}
打印:
countOperate --begain
Thread.currentThread.getName():main
Thread.currentThread.isAlive:true
this.isAlive:false
this.getName:Thread-0
countOperate --end
cunThread.getId:10
run --begain
Thread.currentThread.getName():A
Thread.currentThread.isAlive:true
this.isAlive:false
cunThread.getId:10
this.getName:Thread-0
run --end
cunThread.getId:10
run --begain
Thread.currentThread.getName():B
Thread.currentThread.isAlive:true
this.isAlive:false
cunThread.getId:10
this.getName:Thread-0
run --end
5:Sleep()
在指定毫秒數內讓當前"正在執行的線程"休眠,這個線程是指this.currentThread()返回的線程
public class SleepThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); try { System.err.println("run threadName = "+this.currentThread().getName()+" begain"); Thread.sleep(2000); System.err.println("run threadName = "+this.currentThread().getName()+" end"); } catch (InterruptedException e) { // TODO: handle exception } } } SleepThread th0=new SleepThread(); System.err.println("begain = "+System.currentTimeMillis()); th0.run(); System.err.println("end = "+System.currentTimeMillis());
輸出:
begain = 1521465823061
run threadName = main begain
run threadName = main end
end = 1521465825073
th0.run 變成th0.start();
輸出:
begain = 1521466181414
end = 1521466181414
run threadName = Thread-0 begain
run threadName = Thread-0 end
SleepThread th0=new SleepThread(); Thread th1=new Thread(th0); Thread th2=new Thread(th0); System.err.println("begain = "+System.currentTimeMillis()); th2.setName("A"); th2.start(); th1.setName("B"); th1.start(); System.err.println("end = "+System.currentTimeMillis()); begain = 1521466821288 end = 1521466821288 run threadName = A begain run threadName = B begain run threadName = A end run threadName = B end // this.currentThread().getName() currentThread指執行該改代碼塊的線程 和this區別開來
6:interrupt() 中斷線程
public class InterruptedThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for (int i = 0; i <1000; i++) {
System.out.println("i = "+(i+1));
}
}
}
public class ThreadTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
InterruptedThread th=new InterruptedThread();
th.start();
Thread.sleep(1000);
th.interrupt();
System.out.println("是否中止了1--"+th.interrupted());
System.out.println("是否中止了2--"+th.interrupted());
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
i = 999
i = 1000
是否中止了1--false //th.interrupt 是指main線程
是否中止了2--false
try {
InterruptedThread th=new InterruptedThread();
th.start();
Thread.sleep(1000);
Thread.currentThread().interrupt();
System.out.println("是否中止了1--"+Thread.currentThread().interrupted());
System.out.println("是否中止了2--"+Thread.currentThread().interrupted());
} catch (InterruptedException e) {
// TODO: handle exception
}
i = 999
i = 1000
是否中止了1--true
是否中止了2--false 這裏是中端了main線程
可是倆次調用interrupted() interrupted() 判斷當前線程是否處於中斷狀態,線程的中斷狀態由該方法清除
注意點:
isInterrupted()對比interrupted()沒有清除功能
7:對比
public class InterruptedThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
int i=0;
while (true) {
if(this.isInterrupted()){
System.out.println("線程中止了……");
return;
}
System.out.println("當前時間:"+System.currentTimeMillis());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
InterruptedThread th=new InterruptedThread();
th.start();
Thread.sleep(1000);
th.interrupt();
if(th.interrupted()){
System.out.println("---------------------111");
}
} catch (InterruptedException e) {
// TODO: handle exception
}
}
當前時間:1521472518401
當前時間:1521472518401
當前時間:1521472518401
當前時間:1521472518401
當前時間:1521472518401
當前時間:1521472518401
當前時間:1521472518401
線程中止了……
沒有走System.out.println("---------------------111");