JAVA線程用法詳解
本文配合實(shí)例較為詳細(xì)的講解了Java的線程技術(shù),相信對(duì)于深入理解Java程序設(shè)計(jì)有一定的幫助。具體如下:
很多人在學(xué)習(xí)JAVA時(shí)都對(duì)線程都有一定的了解,而當(dāng)我們開始接觸Android開發(fā)時(shí),才真真正正的發(fā)現(xiàn)了線程是多麼的重要,本文就把對(duì)Java線程的用法心得分享給大家,供大家參考。
首先,大家一定要分清線程和進(jìn)程不是一回事,進(jìn)程是什么呢?進(jìn)程就如我們需要執(zhí)行class文件,而線程才是真正調(diào)用CPU資源來運(yùn)行的。一個(gè)class文件一般只有一個(gè)進(jìn)程,但線程可以有很多個(gè),線程的執(zhí)行是一種異步的執(zhí)行方式。
一、如何在main函數(shù)中再開啟一個(gè)線程:
示例代碼如下:
public class Thread_one {
public static void main(String [] args){
Run run = new Run();
//run.run();//此為方法的調(diào)用,和線程有著天壤之別
Thread thread = new Thread(run);
thread.start();//啟動(dòng)線程,調(diào)用線程的run()方法
for(int i=1; i<=20; i++){
System.out.println("主線程i的 值:--------"+i);
}
}
}
class Run implements Runnable{
@Override
public void run() {
for(int i=1; i<=20; i++){
System.out.println("子線程i的 值:"+i);
}
}
}
二、線程中的sleep方法
示例代碼如下:
public class Thread_sleep {
/*
* 線程停頓
*/
public static void main(String [] args){
Runone run = new Runone();
Thread thread = new Thread(run);
thread.start();
try {
Thread.sleep(5000);
thread.interrupt();//中斷線程的執(zhí)行
//thread.stop();//相對(duì)中斷線程,stop過于粗暴,不建議使用,一般在需要強(qiáng)制關(guān)閉子線程時(shí)方使用此方法
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Runone implements Runnable{
@Override
public void run() {
for(int i=1 ; i<10; i++){
try {
Thread.sleep(1000);
System.out.println("-----"+new Date()+"-----");
} catch (InterruptedException e) {
return ;//當(dāng)捕獲到子線程被中斷時(shí),直接關(guān)閉子線程
}
}
}
}
在這里特別說明一點(diǎn),thread.interrupt();可以中斷線程的執(zhí)行,相對(duì)stop溫柔那么一點(diǎn)點(diǎn),不過還不是最佳的關(guān)閉線程的方法,下面就為大家提供一種方式:
public class Thread_stop {
public static void main(String [] args){
Runthree run = new Runthree();
Thread th = new Thread(run);
th.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
run.setStop();
}
}
class Runthree implements Runnable{
boolean flag;
@Override
public void run() {
flag = true;
int i = 0;
while(flag){
try {
System.out.println("子線程----"+(i++));
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void setStop(){
flag = false;
}
}
下面就簡(jiǎn)單給大家介紹一下線程中的合并和讓出:
一、如何合并線程,此處調(diào)用了join()方法
示例代碼如下:
public class Thread_join {
/*
* 合并線程
*/
public static void main(String [] args){
Runtwo run = new Runtwo();
Thread thread = new Thread(run);
thread.start();
try {
thread.join();//合并線程,此時(shí)相當(dāng)于方法調(diào)用
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0; i<10; i++){
System.out.println("主線程:"+i);
}
}
}
class Runtwo implements Runnable{
@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("子線程:----"+i);
}
}
}
二、如何讓出線程,此處調(diào)用了Thread的yield()方法,如下所示:
public class Thread_yield {
/**讓出CPU
* @param args
*/
public static void main(String[] args) {
Th th = new Th("aaa");
th.start();
for(int i = 0 ; i<=10; i++){
System.out.println("主線程----"+i);
}
}
}
class Th extends Thread{
Th(){}
Th(String s){super(s);}
@Override
public void run() {
for(int i = 0; i<=10; i++){
if(i%3!=0){
System.out.println("子線程"+i);
}else{
System.out.println("子線程i="+i+" 線程進(jìn)行切換");
yield();//從Thread繼承方可使用此方法
}
}
}
}
最后和大家分享一下關(guān)于線程的優(yōu)先級(jí)的問題,代碼如下所示:
public class Thread_priority {
/*
* priority設(shè)置線程的優(yōu)先級(jí)
* Thread默認(rèn)的優(yōu)先級(jí)為5;Thread的最大優(yōu)先級(jí)為10,最小為0
*/
public static void main(String [] args){
T1 t1 = new T1();
T2 t2 = new T2();
t1.start();
//t1.setPriority(Thread.NORM_PRIORITY+3);//設(shè)置t1的優(yōu)先級(jí)
t2.start();
}
}
class T1 extends Thread{
@Override
public void run() {
for(int i = 0; i<50; i++){
System.out.println("線程T1-----"+i);
}
}
}
class T2 extends Thread{
@Override
public void run() {
for(int i = 0; i<50; i++){
System.out.println("線程T2"+i);
}
}
}
相信大家通過以上代碼基本已經(jīng)了解JAVA中的線程機(jī)制,希望本文所述對(duì)大家深入學(xué)習(xí)Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring boot+mybatis+thymeleaf 實(shí)現(xiàn)登錄注冊(cè)增刪改查功能的示例代碼
這篇文章主要介紹了Spring boot+mybatis+thymeleaf 實(shí)現(xiàn)登錄注冊(cè)增刪改查功能的示例代碼,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
java循環(huán)刪除List元素報(bào)錯(cuò)的原因分析與解決
大家在工作中應(yīng)該都會(huì)遇到從List集合中刪除某一個(gè)或多個(gè)元素的業(yè)務(wù)場(chǎng)景,相信大家都會(huì)避開在循環(huán)里面刪除元素,使用其他方式處理,這是為什么呢,下面小編就來和大家詳細(xì)聊聊2023-11-11
SpringBoot傳遞單一參數(shù)時(shí)@RequestParam和@RequestBody的區(qū)別小結(jié)
用SpringBoot框架做項(xiàng)目時(shí),經(jīng)常需要前端給后端傳遞參數(shù),本文主要介紹了SpringBoot傳遞單一參數(shù)時(shí)@RequestParam和@RequestBody的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Mybatis中如何設(shè)置sqlSession自動(dòng)提交
在MyBatis中,默認(rèn)情況下,獲取的SqlSession對(duì)象不會(huì)自動(dòng)提交事務(wù),這意味著在進(jìn)行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來提交事務(wù),但是,可以在獲取SqlSession時(shí)通過將openSession方法的參數(shù)設(shè)置為true2024-09-09
MybatisPlus中QueryWrapper常用方法總結(jié)
MyBatis-Plus是一個(gè)Mybatis增強(qiáng)版工具,在MyBatis上擴(kuò)充了其他功能沒有改變其基本功能,為了簡(jiǎn)化開發(fā)提交效率而存在,queryWrapper是mybatis plus中實(shí)現(xiàn)查詢的對(duì)象封裝操作類,本文就給大家總結(jié)了MybatisPlus中QueryWrapper的常用方法,需要的朋友可以參考下2023-07-07
SpringSecurity授權(quán)機(jī)制的實(shí)現(xiàn)(AccessDecisionManager與投票決策)
本文主要介紹了SpringSecurity授權(quán)機(jī)制的實(shí)現(xiàn),其核心是AccessDecisionManager和投票系統(tǒng),下面就來介紹一下,感興趣的可以了解一下2025-04-04
@PathParam和@QueryParam區(qū)別簡(jiǎn)析
這篇文章主要介紹了@PathParam和@QueryParam區(qū)別,分享了相關(guān)實(shí)例代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

