多執行緒技術概述
執行緒與行程
行程:是指一個記憶體中運行的應用程式,每個行程都有一個獨立的記憶體空間
執行緒:①是行程中的一個執行路徑,共享一個記憶體空間,執行緒之間可以自由切換,并發執行. 一個行程最少
有一個執行緒
②執行緒實際上是在行程基礎之上的進一步劃分,一個行程啟動之后,里面的若干執行路徑又可以劃分
成若干個執行緒
執行緒調度
分時調度
所有執行緒輪流使用 CPU 的使用權,平均分配每個執行緒占用 CPU 的時間,
搶占式調度
優先讓優先級高的執行緒使用 CPU,如果執行緒的優先級相同,那么會隨機選擇一個(執行緒隨機性),Java使用的為搶占式調度,
CPU使用搶占式調度模式在多個執行緒間進行著高速的切換,對于CPU的一個核新而言,某個時刻,只能執行一個執行緒,而 CPU的在多個執行緒間切換速度相對我們的感覺要快,看上去就是 在同一時刻運行, 其實,多執行緒程式并不能提高程式的運行速度,但能夠提高程式運行效率,讓CPU的 使
用率更高,
同步與異步
同步:排隊執行,效率低但是安全,
異步:并行執行,效率高但是不安全,
并發與并行
并發:指兩個或多個事件在同一個時間段內發生,
并行:指兩個或多個事件在同一刻發生(同時發生),
多執行緒技術
實作Runnable 與 繼承Thread相比有如下優勢:
* 1. 通過創建任務,然后給程式分配的方式來實作的多執行緒,更適合多個執行緒同時執行相同的任務的情況
* 2. 可以避免單執行緒所帶來的的局限性
* 3. 任務與執行緒本身是分離的,提高了程式的健壯性
* 4. 后續的執行緒池技術,接收Runnable型別的任務,不接收Thread類
Thread繼承
**實作Thread**
ThreadTest test=new ThreadTest();
test.start();
for (int i = 1; i <100; i++) {
System.out.println("看電視");
}
實作runnable
1.
//1. 創建一個任務物件
MyRunnable mr = new MyRunnable();
//2. 創建一個執行緒并為其分配一個任務
Thread t = new Thread(mr);
//執行這個執行緒
t.start();
for (int i = 1; i <100; i++) {
System.out.println("看電視ing");
}
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("吃飯ing");
}
}
}
設定和獲取執行緒名稱
public class ThreadSetGet {
public static void main(String[] args) {
//獲取當前運行的執行緒的物件名稱
System.out.println(Thread.currentThread().getName());
new Thread(new MyTest(),"李狗蛋1號").start();
new Thread(new MyTest(),"李狗蛋2號").start();
new Thread(new MyTest(),"李狗蛋3號").start();
}
static class MyTest implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
}
1.執行緒休眠sleep
for (int i = 0; i < 10; i++) {
System.out.println(i);
//每次回圈休眠1秒種 以毫秒為單位
Thread.sleep(1000);
}
2.執行緒阻塞
堵在那里等待運行完成
3.執行緒中斷
打入一個中斷標記
4.守護執行緒
守護用戶執行緒,當最后一個用戶執行緒結束時,所有守護執行緒自動死亡,
- 執行緒:分為守護執行緒和用戶執行緒
- 用戶執行緒:當一個行程不包含任何的存活的用戶執行緒時,運行結束,
Thread t1=new Thread();
//設定為守護執行緒
t1.setDaemon(true);
t1.start();
for (int i = 0; i < 5; i++) {
System.out.println(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(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//e.printStackTrace();
System.out.println("發現中斷標記,自殺死亡");
//回傳終止
return;
}
}
}
執行緒安全問題
public class SaftyProblem {
public static void main(String[] args) {
Runnable runnable=new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).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);
}
}
//最后導致三個執行緒同時處在回圈中賣最后一張票時導致出現負票數
}
}
解決方案1. 同步代碼塊
格式:synchronized (鎖物件){}
public class Demo001 {
public static void main(String[] args) {
Runnable runnable = new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票數
private int count = 10;
private Object o = new Object();
@Override
public void run() {
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;
}
}
}
}
}
}
解決方案2. 同步方法
public class Demo002 {
public static void main(String[] args) {
/*
* 解決方案2. 同步方法
* */
Runnable runnable = new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票數
private int count = 10;
@Override
public void run() {
//賣票
while (true) {
boolean flag = sale();
if (!flag) {
break;
}
}
}
private synchronized boolean sale() {
while (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;
}
}
}
解決方案3. 顯示鎖Lock 子類 ReentrantLock
public class Demo003 {
public static void main(String[] args) {
/*
* 解決方案2. 同步方法
* */
Runnable runnable = new Ticket();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
static class Ticket implements Runnable {
//票數
private int count = 10;
//顯示鎖 fair的引數為true就是公平鎖:誰先來誰先的到這個鎖
private Lock l=new ReentrantLock();
@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();
}
}
}
}
執行緒死鎖
A等B B等A
例子:罪犯與警察↓↓↓
public class Demo004 {
/*
* 執行緒死鎖 A等B B等A
* */
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;
public 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("警察放了罪犯,并且罪犯跑了");
}
}
}
多執行緒通信問題
例子: 生產者與消費者問題↓↓↓
public class Demo5 {
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表示可以生產
private 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();
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/271252.html
標籤:其他
上一篇:http頭部引數、http與https區別、localhost與127.0.0.1、phpStudy搭建dvwa、SEO搜索引擎劫持、C段旁段、子域名查詢
下一篇:VC實作將網址決議出所有ip地址
