前言:本系列將從零開始講解java多線程相關的技術,內容參考於《java多線程核心技術》與《java併發編程實戰》等相關資料,但願站在巨人的肩膀上,再經過個人理解能讓知識更加簡單易懂。html
public class T1 { public static void main(String[] args) { PrivateNum p=new PrivateNum(); MyThread threadA=new MyThread('A',p); MyThread threadB=new MyThread('B',p); threadA.start(); threadB.start(); }} class MyThread extends Thread { char i; PrivateNum p; public MyThread(char i,PrivateNum p) { this.i=i; this.p=p; } public void run() { p.test(i); } } class PrivateNum { public void test( char i) { try { int num=0; if(i=='A') { num=100; System.out.println("線程A已經設置完畢"); Thread.sleep(1000); } else { num=200; System.out.println("線程B已經設置完畢"); } System.out.println("線程"+i+"的值:"+num); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); }} } 線程A已經設置完畢 線程B已經設置完畢 線程B的值:200 線程A的值:100
public void test( char i) { int num=0; try { if(i=='A') { num=100; System.out.println("線程A已經設置完畢"); Thread.sleep(1000); } else { num=200; System.out.println("線程B已經設置完畢"); } System.out.println("線程"+i+"的值:"+num); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); }} 線程B已經設置完畢 線程A已經設置完畢 線程B的值:200 線程A的值:200
int num=0
放到了方法的外面,可是輸出的結果卻不一樣,由於這時候線程AB訪問的是同一個變量(指向同一個地址),因此這個時候會發生覆蓋,同時這裏出現了線程安全問題。public synchronized void test( char i) { int num=0; try { if(i=='A') { num=100; System.out.println("線程A已經設置完畢"); Thread.sleep(1000); } else { num=200; System.out.println("線程B已經設置完畢"); } System.out.println("線程"+i+"的值:"+num); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); }} 線程A已經設置完畢 線程A的值:100 線程B已經設置完畢 線程B的值:200
synchronized
,避免了線程安全問題public class T1 { public static void main(String[] args) { PrivateNum p1=new PrivateNum(); PrivateNum p2=new PrivateNum(); MyThread threadA=new MyThread('A',p1); MyThread threadB=new MyThread('B',p2); threadA.start(); threadB.start(); }} 線程A已經設置完畢 線程B已經設置完畢 線程B的值:200 線程A的值:100
線程A已經設置完畢 線程A的值:100
),這是由於這裏是兩個鎖,建立了p1和p2對象,建立的是兩個鎖,鎖對象不一樣不形成互斥做用。public class T1 { public static void main(String[] args) { PrivateNum p1=new PrivateNum(); MyThread threadA=new MyThread('A',p1); MyThread2 threadB=new MyThread2('B',p1); threadA.start(); threadB.start(); }} class MyThread extends Thread { char i; PrivateNum p; public MyThread(char i,PrivateNum p) { this.i=i; this.p=p; } public void run() { p.test(i); } } class MyThread2 extends Thread { char i; PrivateNum p; public MyThread2(char i,PrivateNum p) { this.i=i; this.p=p; } public void run() { p.test2(i); } } class PrivateNum { int num=0; public void test2(char i) { System.out.println("線程"+i+"執行,線程A並無同步執行"); } public synchronized void test( char i) { try { if(i=='A') { num=100; System.out.println("線程A已經設置完畢"); Thread.sleep(100); } else { num=200; System.out.println("線程B已經設置完畢"); } System.out.println("線程"+i+"的值:"+num); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); }} } 線程A已經設置完畢 線程B執行,線程A並無同步執行 線程A的值:100 線程B的值:200
public synchronized void test2(char i) { System.out.println("線程"+i+"執行,線程A同步執行"); } 線程A已經設置完畢 線程A的值:100 線程B執行,線程A同步執行 線程B的值:200
public class T1 { public static void main(String[] args) { MyThread3 thread=new MyThread3(); thread.start(); }} class MyThread3 extends Thread { Service s=new Service(); public void run() { s.service1(); } } class Service { public synchronized void service1() { System.out.println("服務1並無被鎖住"); service2(); } public synchronized void service2() { System.out.println("服務2並無被鎖住"); service3(); } public synchronized void service3() { System.out.println("服務3並無被鎖住"); } } 服務1並無被鎖住 服務2並無被鎖住 服務3並無被鎖住
public class T1 { public static void main(String[] args) { Service3 s=new Service3(); MyThread4 t1=new MyThread4(s,'1'); MyThread4 t2=new MyThread4(s,'2'); t1.start(); t2.start(); }} class MyThread4 extends Thread { Service3 s; char name; public MyThread4(Service3 s,char name) { this.s=s; this.name=name; } public void run() { s.service(name); } } class Service2 { public synchronized void service(char name) { for (int i = 3; i >0; i--) { System.out.println(i); } } } class Service3 extends Service2 { public void service(char name) { for (int i = 5; i >0; i--) { System.out.println("線程"+name+":"+i); } } } 線程1:5 線程2:5
public class T1 { public static void main(String[] args) { Service2 s=new Service2(); MyThread t1=new MyThread(s,'A'); MyThread t2=new MyThread(s,'B'); t1.start(); t2.start(); } } class Service2 { public void service(char name) { synchronized(this) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } } class MyThread extends Thread { Service2 s=new Service2(); char name; public MyThread(Service2 s,char name) { this.s=s; this.name=name; } public void run() { s.service(name); } } A:3 A:2 A:1 B:3 B:2 B:1
class Service2 { String s=new String("鎖"); public void service(char name) { synchronized(s) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } }
public void service(char name) { for (int i = 6; i >3; i--) { System.out.println(name+":"+i); } synchronized(this) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } A:6 B:6 A:5 B:5 A:4 B:4 A:3 A:2 A:1 B:3 B:2 B:1
public class T1 { public static void main(String[] args) { Service2 s=new Service2(); MyThread t1=new MyThread(s,'A'); MyThread2 t2=new MyThread2(s,'B'); t1.start(); t2.start(); } } class Service2 { public void service(char name) { synchronized(this) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } public void service2(char name) { synchronized(this) { for (int i = 6; i >3; i--) { System.out.println(name+":"+i); } } } } class MyThread extends Thread { Service2 s=new Service2(); char name; public MyThread(Service2 s,char name) { this.s=s; this.name=name; } public void run() { s.service(name); } } class MyThread2 extends Thread { Service2 s=new Service2(); char name; public MyThread2(Service2 s,char name) { this.s=s; this.name=name; } public void run() { s.service2(name); } } A:3 A:2 A:1 B:6 B:5 B:4
class Service2 { Strign s=new String(); public void service(char name) { synchronized(s) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } public void service2(char name) { synchronized(this) { for (int i = 6; i >3; i--) { System.out.println(name+":"+i); } } } }
public void service(char name) { synchronized(this) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } public synchronized void service2(char name) { for (int i = 6; i >3; i--) { System.out.println(name+":"+i); } }
class Service2 { public synchronized static void service() { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } }
class Service2 { public static void service() { synchronized(Service.class) { for (int i = 3; i >0; i--) { System.out.println(name+":"+i); } } } }
public class T1 { public static void main(String[] args) { Service.Service2 s=new Service.Service2(); Thread t1=new Thread(new Runnable() {public void run(){s.service();}}); Thread t2=new Thread(new Runnable() {public void run(){Service.Service2.service2();}}); t1.start(); t2.start(); } } class Service{ static class Service2 { public synchronized void service() { for (int i = 20; i >10; i--) { System.out.println(i); } } public static synchronized void service2() { for (int i = 9; i >3; i--) { System.out.println(i); } } }} //不一樣步執行
public class DealThread implements Runnable { public String username; public Object lock1 = new Object(); public Object lock2 = new Object(); public void setFlag(String username) { this.username = username; } @Override public void run() { if (username.equals("a")) { synchronized (lock1) { try { System.out.println("username = " + username); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (lock2) { System.out.println("按lock1->lock2代碼順序執行了"); } } } if (username.equals("b")) { synchronized (lock2) { try { System.out.println("username = " + username); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (lock1) { System.out.println("按lock2->lock1代碼順序執行了"); } } } } }
public class Tee { public static void main(String[] args) { try { RunThread thread = new RunThread(); thread.start(); Thread.sleep(1000); thread.setRunning(false); System.out.println("已經賦值爲false"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class RunThread extends Thread { private boolean isRunning = true; public boolean isRunning() { return isRunning; } public void setRunning(boolean isRunning) { this.isRunning = isRunning; } @Override public void run() { System.out.println("進入run了"); while (isRunning == true) { } System.out.println("線程被中止了!"); } }
public class Tee { public static void main(String[] args) { MyThread[] mythreadArray = new MyThread[100]; for (int i = 0; i < 100; i++) { mythreadArray[i] = new MyThread(); } for (int i = 0; i < 100; i++) { mythreadArray[i].start(); } } class MyThread extends Thread { volatile public static int count; private static void addCount() { for (int i = 0; i < 100; i++) { count++; } System.out.println("count=" + count); } @Override public void run() { addCount(); } }
class MyThread extends Thread { static AtomicInteger count=new AtomicInteger(0); private static void addCount() { for (int i = 0; i < 100; i++) { count.incrementAndGet(); } System.out.println(count.get()); } @Override public void run() { addCount(); } }
做者:jiajun 出處: http://www.cnblogs.com/-new/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。java