进程:进程是操做系统结构的基础;是一次程序的执行;是一个程序及其数据在处理机上顺序执行时所发生的活动;是程序在一个数据集合上运行的过程,它是系统进行资源分配和调度的一个独立单位;进程是受操做系统管理的基本运行单元。java
线程:能够理解成是在进程中独立运行的子任务;使用多线程也就是在使用异步。线程分为两种一种是用户线程,一种是守护线程;守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁(垃圾回收线程),setDaemon(true)设置线程为守护线程。多线程
package chapter1.create; public class MyThread extends Thread{ @Override public void run() { super.run(); System.out.println("MyThread"); } }
package chapter1.create; public class Run { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); System.out.println("end!"); } }
运行结果以下:异步
end!
MyThreadide
线程是一个子任务,CUP以不肯定的方式,或者说是随机的时间来调用线程中的run方法,因此就会出现先打印end!,再打印MyThread了。this
package chapter1.create; public class MyRunnable implements Runnable{ @Override public void run() { System.out.println("运行中..."); } }
package chapter1.create; public class Run { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); System.out.println("运行结束"); } }
运行结果:spa
运行结束
运行中...
Thread.java类也实现了Runnable接口,也就意味着Thread(myRunnable)能够传入一个Thread类的对象,这样作能够将一个Thread对象中的run方法交由其余的线程进行调用。操作系统
使用继承Thread类的方式建立线程时,最大的局限就是不支持多继承。线程
package chapter1.create; public class MehthodCurrentThreadTest { static class CountOperate extends Thread{ public CountOperate() { System.out.println("CountOperate init----begin"); System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName()); System.out.println("this.getName()="+this.getName()); System.out.println("CountOperate init----end"); } @Override public void run() { super.run(); System.out.println("run----begin"); System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName()); System.out.println("this.getName()="+this.getName()); System.out.println("run----end"); } } public static void main(String[] args) { CountOperate countOperate = new CountOperate(); Thread t = new Thread(countOperate); t.setName("A"); t.start(); } }
运行结果:code
CountOperate init----begin
Thread.currentThread().getName()=main
this.getName()=Thread-0
CountOperate init----end
run----begin
Thread.currentThread().getName()=A
this.getName()=Thread-0
run----end对象
判断线程是否为中断状态的方法:
中止线程的方法:
package chapter1.create.threadinterrupt; public class MyThread extends Thread{ @Override public void run() { super.run(); for(int i=0;i<500000;i++) { System.out.println("i="+(i+1)+"interrupted------------"+this.isInterrupted()); } } }
package chapter1.create.threadinterrupt; public class Run { public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (Exception e) { System.out.println("main catch."); e.printStackTrace(); } } }
运行结果:
i=499992interrupted------------true
i=499993interrupted------------true
i=499994interrupted------------true
i=499995interrupted------------true
i=499996interrupted------------true
i=499997interrupted------------true
i=499998interrupted------------true
i=499999interrupted------------true
i=500000interrupted------------true
在沉睡中中止:
package chapter1.create.threadinterrupt; public class StopWhenSleepTest { static 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("进MyThread.java类run方法中的catch了。"+this.isInterrupted()); e.printStackTrace(); } } } public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (Exception e) { System.out.println("main catch."); e.printStackTrace(); } System.out.println("Main end"); } }
运行结果:
run begin
Main end
进MyThread.java类run方法中的catch了。false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at chapter1.create.threadinterrupt.StopWhenSleepTest$MyThread.run(StopWhenSleepTest.java:12)
若是在sleep状态下中止线程,会进入catch,而且清楚中止状态值,使之成为false。先interrupt再sleep也会进入catch,此处不作赘述。
package chapter1.create.threadinterrupt; public class StopThreadByExceptionTest { static class MyThread extends Thread{ @Override public void run() { super.run(); try { for(int i=0;i<500000;i++) { if(this.isInterrupted()) { System.out.println("已是中止状态了!我要退出了"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } } catch (InterruptedException e) { System.out.println("进MyThread.java类run方法中的catch了。"); e.printStackTrace(); } } } public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (Exception e) { System.out.println("main catch."); e.printStackTrace(); } } }
运行结果:
i=420003
i=420004
i=420005
i=420006
i=420007
i=420008
i=420009
i=420010
i=420011
已是中止状态了!我要退出了
进MyThread.java类run方法中的catch了。
java.lang.InterruptedException
at chapter1.create.threadinterrupt.StopThreadByExceptionTest$MyThread.run(StopThreadByExceptionTest.java:14)
setPriority(int priority):值的范围1-10,设置线程优先级有助于帮“线程规划器”肯定在下一次选择哪个线程来优先执行。