前言:本系列將從零開始講解java多線程相關的技術,內容參考於《java多線程核心技術》與《java併發編程實戰》等相關資料,但願站在巨人的肩膀上,再經過個人理解能讓知識更加簡單易懂。html
public class T1 { public static void main(String[] args) { MyThread myThread=new MyThread(); myThread.start(); System.out.println("代碼的執行結果與代碼的順序無關"); } } class MyThread extends Thread { public void run() { System.out.println("建立的線程"); } }
public class T1 { public static void main(String[] args) { MyThread myThread=new MyThread(); myThread.run(); System.out.println("若是是直接執行run方法,確定是按代碼順序執行的,由於是經過主線程調用的"); } } class MyThread extends Thread { public void run() { System.out.println("建立的線程"); } }
public class MyRunnable implements Runnable { @Override public void run() { System.out.println("運行中!"); } } public class Run { public static void main(String[] args) { Runnable runnable=new MyRunnable(); Thread thread=new Thread(runnable); thread.start(); System.out.println("運行結束!"); } }
public static void main(String[] args) { MyThread a = new MyThread("A"); MyThread b = new MyThread("B"); MyThread c = new MyThread("C"); a.start(); b.start(); c.start(); } class MyThread extends Thread { private int count = 5; public MyThread(String name) { super(); this.setName(name); } @Override public void run() { super.run(); while (count > 0) { count--; System.out.println("由 " + this.currentThread().getName() + " 計算,count=" + count); } } }
public static void main(String[] args) { MyThread mythread=new MyThread(); //線程a b c啓動的時候,執行的是myThread的方法,此時數據共享 Thread a=new Thread(mythread,"A"); Thread b=new Thread(mythread,"B"); Thread c=new Thread(mythread,"C"); a.start(); b.start(); c.start(); }
public synchronized void run() { super.run(); count--; System.out.println("由 "+this.currentThread().getName()+" 計算,count="+count);//輸出的必定是4 3 2 }
public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(200); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 0; i < 500000; i++) { System.out.println("i=" + (i + 1)); } } }
public class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 0; i < 500000; i++) { System.out.println("i=" + (i + 1)); } } } public class Run { public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(1000); thread.interrupt(); //Thread.currentThread().interrupt(); System.out.println("是否中止1?="+thread.interrupted());//false System.out.println("是否中止2?="+thread.interrupted());//false main線程沒有被中斷!!! //......
public class Run { public static void main(String[] args) { try { Thread.currentThread().interrupt(); System.out.println("是否中止1?="+Thread.interrupted());//true System.out.println("是否中止2?="+Thread.interrupted());//false //......
public static void main(String[] args) { MyThread thread=new MyThread(); thread.start(); thread.interrupt(); System.out.println(thread.isInterrupted());//true System.out.println(thread.isInterrupted());//true }
import exthread.MyThread; import exthread.MyThread; public class Run { public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } } public class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 0; i < 500000; i++) { if (this.interrupted()) { System.out.println("已是中止狀態了!我要退出了!"); break; } System.out.println("i=" + (i + 1)); } System.out.println("666"); } }
public class MyThread extends Thread { @Override public void run() { super.run(); try { for (int i = 0; i < 500000; i++) { if (this.interrupted()) { System.out.println("已是中止狀態了!我要退出了!"); throw new InterruptedException(); } System.out.println("i=" + (i + 1)); } System.out.println("我在for下面"); } catch (InterruptedException e) { System.out.println("進MyThread.java類run方法中的catch了!"); e.printStackTrace(); } } }
for (int i = 0; i < 500000; i++) { if (this.interrupted()) { System.out.println("已是中止狀態了!我要退出了!"); return; } System.out.println("i=" + (i + 1)); }
public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(200); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } } class MyThread extends Thread { @Override public void run() { super.run(); try { System.out.println("run begin"); Thread.sleep(200000); System.out.println("run end"); } catch (InterruptedException e) { System.out.println("在沉睡中被中止!進入catch!"+this.isInterrupted()); e.printStackTrace(); } } }*/
public static void main(String[] args) { try { final SynchronizedObject object = new SynchronizedObject(); Thread thread1 = new Thread() { @Override public void run() { object.printString(); } }; thread1.setName("a"); thread1.start(); Thread.sleep(1000); Thread thread2 = new Thread() { @Override public void run() { System.out .println("thread2啓動了,但進入不了printString()方法!只打印1個begin"); System.out .println("由於printString()方法被a線程鎖定而且永遠的suspend暫停了!"); object.printString(); } }; thread2.start(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class SynchronizedObject { synchronized public void printString() { System.out.println("begin"); if (Thread.currentThread().getName().equals("a")) { System.out.println("a線程永遠 suspend了!"); Thread.currentThread().suspend(); } System.out.println("end"); } }
public void println(String x) { synchronized (this) { print(x); newLine(); } }
public static void main(String[] args) { System.out.println("main thread begin priority=" + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(6); System.out.println("main thread end priority=" + Thread.currentThread().getPriority()); MyThread1 thread1 = new MyThread1(); thread1.start(); } class MyThread1 extends Thread { @Override public void run() { System.out.println("MyThread1 run priority=" + this.getPriority()); MyThread2 thread2 = new MyThread2(); thread2.start(); } }
做者:jiajun 出處: http://www.cnblogs.com/-new/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。java