0.基本概念
0.1行程:是指一個記憶體中運行的應用程式(現在很多軟體是多行程的),每個行程都有一個獨立的記憶體空間,
0.2執行緒:是行程中的一個執行路徑,共享一個記憶體空間,執行緒之間可以自由切換,并發執行,
0.3調度
思考:假設計算機是四核八執行緒,如何將正在執行的1000+個執行緒分配出去,
1.分時調度:所有執行緒輪流使用CPU的使用權,平均分配每個執行緒占用CPU的時間,
2.搶占式調度
優先讓優先級高的執行緒使用CPU,如果一樣高,則隨機,JAVA使用的是搶占式調度,
0.4同步與異步
同步:執行緒排隊執行,效率低,但是安全,
異步:執行緒同時執行,效率高,但是資料不安全,
0.5并發與并行
并發:指兩個或多個事件在同一個時間段內發生,
并行:指兩個或多個事件在同一時刻發生,
0.6每個執行緒都擁有自己的堆疊空間,共用一份堆記憶體,由一個執行緒所呼叫的方法,那么這個方法也會執行在這個執行緒里面,
1.兩種常見創建執行緒的方法
1.繼承Thread
package thread;
public class MyThread extends Thread {
//run方法就是執行緒要執行的任務方法
@Override
public void run() {
//這里的代碼就是一條新的執行路徑
//這個執行路徑是觸發方式,不是呼叫run方法,而是通過thread物件的start方法來啟動任務
for (int i = 0; i < 10; i++) {
System.out.println("鋤禾日當午"+i);
}
}
}
package thread;
public class Demo1 {
public static void main(String[] args) {
MyThread m = new MyThread();
m.start();
for (int i = 0; i < 10; i++) {
System.out.println("汗滴禾下土"+i);
}
}
}
2.實作Runnable介面
package thread;
public class MyRunnable implements Runnable{
@Override
public void run() {
//執行緒的任務
for (int i = 0; i < 10; i++) {
System.out.println("鋤禾日當午"+i);
}
}
}
package thread;
public class Demo1 {
public static void main(String[] args) {
//實作runnable
//1 創建一個任務物件
MyRunnable r = new MyRunnable();
//創建一個執行緒并給他一個任務
Thread t = new Thread(r);
//啟動執行緒
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("汗滴禾下土"+i);
}
}
}
3.實作Runnable與繼承Thread相比有如下優勢
1.通過創建任務,然后給執行緒分配任務的方式實作多執行緒,更適合多個執行緒同時執行任務的情況
2,可以避免單繼承所帶來的局限性
3,任務與執行緒是分離的,提高了程式的健壯性
4,后期學習的執行緒池技術,接受Runnable型別的任務,不接受Thread型別的執行緒
4.實作Runnable的匿名內部類寫法
package thread;
public class Demo2 {
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("12345"+i);
}
}
}.start();
for (int i = 0; i < 10; i++) {
System.out.println("汗滴禾下土"+i);
}
}
}
5.給執行緒命名的代碼示例
package thread;
public class Demo3 {
public static void main(String[] args) {
//如何獲取執行緒的名稱
System.out.println(Thread.currentThread().getName());
//兩種設定執行緒名稱的方式
Thread t = new Thread(new MyRunnable());
t.setName("wwww");
t.start();
new Thread(new MyRunnable(),"鋤禾日當午").start();
//不設定的有默認的名字
new Thread(new MyRunnable()).start();
}
static class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
}
6.執行緒休眠sleep
package thread;
public class Demo4 {
public static void main(String[] args) throws InterruptedException {
//執行緒的休眠
for (int i = 0; i < 10; i++) {
System.out.println(i);
Thread.sleep(1000); //1000毫秒
}
}
}
7.執行緒阻塞
不只是執行緒休眠,理解為所有消耗時間的操作,比如:檔案讀取,接收用戶輸入,
8.執行緒中斷
一個執行緒是一個獨立的執行路徑,它是否應該結束,應該由其自身決定,給執行緒T添加中斷標記,在sleep和wait等會檢查標記,
package thread;
public class Demo5 {
public static void main(String[] args) {
//執行緒中斷
//y一個執行緒是一個獨立的執行路徑,它是否結束應該由其自身決定
Thread t1 = new Thread(new MyRunnable());
t1.start();
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//給執行緒t1添加中斷標記
t1.interrupt();
}
static class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//e.printStackTrace();
System.out.println("發現了中斷標記,執行緒自殺");
return;
}
}
}
}
}
9.用戶執行緒、守護執行緒
package thread;
public class Demo6 {
public static void main(String[] args) {
//執行緒分為守護執行緒和用戶執行緒
//用戶執行緒:當一個行程不包含任何的存活的用戶執行緒時,進行結束
//守護執行緒:守護用戶執行緒的,當最后一個用戶執行緒結束時,所有守護執行緒自動死亡,
Thread t1 = new Thread(new MyRunnable());
//設定守護執行緒
t1.setDaemon(true);
t1.start();
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
10.執行緒不安全
package thread;
public class Demo7 {
public static void main(String[] args) {
//執行緒不安全
Runnable run = new Ticket();
new Thread(run).start();
new Thread(run).start();
new Thread(run).start();
}
static class Ticket implements Runnable{
//總票數
private int count = 10;
@Override
public void run() {
while (count>0){
//賣票
System.out.println("正在準備賣票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println("賣票結束,余票:"+count);
}
}
}
}
11執行緒安全1(同步代碼塊)
package thread;
//執行緒同步synchronized
public class Demo8 {
public static void main(String[] args) {
Object o = new Object();
//執行緒不安全
//解決方案1 同步代碼塊
//格式:synchronized(鎖物件){
//
//
// }
Runnable run = new Ticket();
new Thread(run).start();
new Thread(run).start();
new Thread(run).start();
}
static class Ticket implements Runnable{
//總票數
private int count = 10;
private Object o = new Object();
@Override
public void run() {
//Object o = new Object(); //這里不是同一把鎖,所以鎖不住
while (true) {
synchronized (o) {
if (count > 0) {
//賣票
System.out.println("正在準備賣票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName()+"賣票結束,余票:" + count);
}else {
break;
}
}
}
}
}
}
12.執行緒安全2(同步方法)
package thread;
//執行緒同步synchronized
public class Demo9 {
public static void main(String[] args) {
Object o = new Object();
//執行緒不安全
//解決方案2 同步方法
Runnable run = new Ticket();
new Thread(run).start();
new Thread(run).start();
new Thread(run).start();
}
static class Ticket implements Runnable{
//總票數
private int count = 10;
@Override
public void run() {
while (true) {
boolean flag = sale();
if(!flag){
break;
}
}
}
public synchronized boolean sale(){
if (count > 0) {
//賣票
System.out.println("正在準備賣票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName()+"賣票結束,余票:" + count);
return true;
}
return false;
}
}
}
13執行緒安全3(顯式鎖Lock)
package thread;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//同步代碼塊和同步方法都屬于隱式鎖
//執行緒同步lock
public class Demo10 {
public static void main(String[] args) {
Object o = new Object();
//執行緒不安全
//解決方案1 顯示鎖 Lock 子類 ReentrantLock
Runnable run = new Ticket();
new Thread(run).start();
new Thread(run).start();
new Thread(run).start();
}
static class Ticket implements Runnable{
//總票數
private int count = 10;
//引數為true表示公平鎖 默認是false 不是公平鎖
private Lock l = new ReentrantLock(true);
@Override
public void run() {
while (true) {
l.lock();
if (count > 0) {
//賣票
System.out.println("正在準備賣票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName()+"賣票結束,余票:" + count);
}else {
break;
}
l.unlock();
}
}
}
}
14執行緒死鎖
注意:在任何有可能導致鎖產生的方法里,不要再呼叫另外一個方法讓另外一個鎖產生,
package thread;
public class Demo11 {
public static void main(String[] args) {
//執行緒死鎖
Culprit c = new Culprit();
Police p = new Police();
new MyThread(c,p).start();
c.say(p);
}
static class MyThread extends Thread{
private Culprit c;
private Police p;
MyThread(Culprit c,Police p){
this.c = c;
this.p = p;
}
@Override
public void run() {
p.say(c);
}
}
static class Culprit{
public synchronized void say(Police p){
System.out.println("罪犯:你放了我,我放了人質");
p.fun();
}
public synchronized void fun(){
System.out.println("罪犯被放了,罪犯也放了人質");
}
}
static class Police{
public synchronized void say(Culprit c){
System.out.println("警察:你放了人質,我放了你");
c.fun();
}
public synchronized void fun(){
System.out.println("警察救了人質,但是罪犯跑了");
}
}
}
15.多執行緒通信問題
package thread;
public class Demo12 {
public static void main(String[] args) {
//多執行緒通信 生產者與消費者問題
Food f = new Food();
new Cook(f).start();
new Waiter(f).start();
}
//廚師
static class Cook extends Thread{
private Food f;
public Cook(Food f) {
this.f = f;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(i%2==0){
f.setNameAndTaste("老干媽小米粥","香辣味");
}else {
f.setNameAndTaste("煎餅果子","甜辣味");
}
}
}
}
//服務員
static class Waiter extends Thread{
private Food f;
public Waiter(Food f) {
this.f = f;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
f.get();
}
}
}
//食物
static class Food{
private String name;
private String taste;
//true表示可以生產
boolean flag = true;
public synchronized void setNameAndTaste(String name,String taste){
if(flag){
this.name = name;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.taste = taste;
flag = false;
this.notifyAll();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void get(){
if(!flag){
System.out.println("服務員端走的菜的名稱是:"+name+",味道是:"+taste);
flag = true;
this.notifyAll();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
16.用的不多的知識點:
帶回傳值的執行緒Callable、四種執行緒池:緩沖執行緒池(長度無限制)、定長執行緒池、單執行緒執行緒池、周期性任務定長執行緒池,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/161388.html
標籤:其他
