最近在看JAVA教學的視頻,覺得老師講的很好,同時借用源代碼還有筆記來撰寫本系列博客,記錄自己的學習內容,同時也供看到的人學習。
本篇介紹三大部分內容,首先來看第一部分:線程的狀態
下圖是線程狀態的轉換圖:
線程各個狀態的具體介紹以及在JAVA中的使用方法:
第二個內容是介紹線程的停止:
1、自然終止:線程體正常執行完畢
2、外部干涉:
1)、線程類中 定義 線程體使用的標識
2)、線程體使用該標識
3)、提供對外的方法改變該標識
4)、外部根據條件調用該方法即可
下面是實例代碼:public class StopDemo01 {
public static void main(String[] args) {
Study s =new Study();
new Thread(s).start(); //使用匿名類
//外部干涉
for(int i=0;i<100;i++){
if(50==i){ //外部干涉
s.stop();
}
System.out.println("main.....-->"+i);
}
}
}
class Study implements Runnable{
//1)、線程類中 定義 線程體使用的標識
private boolean flag =true;
@Override
public void run() {
//2)、線程體使用該標識
while(flag){
System.out.println("study thread....");
}
}
//3)、對外提供方法改變標識
public void stop(){
this.flag =false;
}
}
第三個內容是線程的阻塞:
1、join :合併線程
2、yield:暫停自己的線程 static
3、sleep:休眠,不釋放鎖
1)、與時間相關:倒計時
2)、模擬網絡延時
NO.1:合併進程的示例代碼:/**
* join:合併線程
*/
public class JoinDemo01 extends Thread {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
JoinDemo01 demo = new JoinDemo01();
Thread t = new Thread(demo); //新生
t.start();//就緒
//cpu調度 運行
for(int i=0;i<1000;i++){
if(50==i){
t.join(); //main方法阻塞...,50之後就只會進行run的,run的結束後再執行main的
}
System.out.println("main...."+i);
}
}
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("join...."+i);
}
}
}
NO.2:yield的示例代碼:
public class YieldDemo01 extends Thread {
public static void main(String[] args) {
YieldDemo01 demo = new YieldDemo01();
Thread t = new Thread(demo); //新生
t.start();//就緒
//cpu調度 運行
for(int i=0;i<1000;i++){
if(i%20==0){
//暫停本線程 main(規則:寫在哪個方法裏面就暫停哪個方法)
Thread.yield();
}
System.out.println("main...."+i);
}
}
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("yield...."+i);
}
}
}
NO.3:sleep的兩段示例代碼:
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 倒計時
* 1、倒數10個數,一秒內打印一個
* 2、倒計時
*/
public class SleepDemo01 {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Date endTime =new Date(System.currentTimeMillis()+10*1000);
long end =endTime.getTime();
while(true){
//輸出
System.out.println(new SimpleDateFormat("mm:ss").format(endTime));
//等待一秒
Thread.sleep(1000);
//構建下一秒時間
endTime =new Date(endTime.getTime()-1000);
//10秒以內 繼續 否則 退出
if(end-10000>endTime.getTime()){
break;
}
}
}
public static void test1() throws InterruptedException{
int num =10;
while(true){
System.out.println(num--);
Thread.sleep(1000); //暫停
if(num<=0){
break;
}
}
}
}
/**
* Sleep模擬 網絡延時 線程不安全的類
*
*/
public class SleepDemo02 {
/**
* @param args
*/
public static void main(String[] args) {
//真實角色
Web12306 web= new Web12306();
Web12306 web2 = new Web12306();
//代理
Thread t1 =new Thread(web,"路人甲");
Thread t2 =new Thread(web,"黃牛已");
Thread t3 =new Thread(web,"攻城師");
//啓動線程
t1.start();
t2.start();
t3.start();
}
}
class Web12306 implements Runnable {
private int num =50;
@Override
public void run() {
while(true){
if(num<=0){
break; //跳出循環
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"搶到了"+num--);
}
}
}
到這片博客滿50篇了,也達到了寒假的計劃要求···