前言:本系列将从零开始讲解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