代码如下:
//1. 继承thread类,重写run方法,run方法中,需要线程执行代码 class threaddemo01 extends thread { // run方法中,需要线程需要执行代码 @override public void run() { for (int i = 0; i < 10; i++) { system.out.print("子线程id:" + this.getid() + ","); system.out.print("子线程name:" + getname()+","); system.out.println("子线程--->i:" + i); } } } // 1.什么是线程 线程是一条执行路径,每个线程都互不影响。 // 2.什么是多线程,多线程在一个进程中,有多条不同的执行路径,并行执行。目的为了提高程序效率。 // 3.在一个进程中,一定会主线程。 // 4.如果连线程主线程都没有,怎么执行程序。 // 线程几种分类 1. 用户线程、守护线程 // 2. 主线程 子线程 gc线程 public class t001_createwiththread { // 交替执行 public static void main(string[] args) { system.out.println("main... 主线程开始..."); // 1.创建线程 threaddemo01 threaddemo01 = new threaddemo01(); // 2.启动线程 threaddemo01.start(); for (int i = 0; i < 10; i++) { system.out.println("main---> i: " + i); } system.out.println("main... 主线程结束..."); } }
执行结果:
匿名内部类的方式
system.out.println("-----多线程创建开始-----"); new thread() { public void run() { for (int i = 0; i < 10; i++) { system.out.println("线程 -- " + thread.currentthread().getname() + "-->" + i); } }; }.start(); system.out.println("-----多线程创建结束-----");
代码如下:
class threaddemo02 implements runnable { public void run() { for (int i = 0; i < 10; i++) { system.out.println(" 子 i:" + i); } } } // 1.实现runable接口,重写run方法 public class t002_createwithrunnable { public static void main(string[] args) { system.out.println("main... 主线程开始..."); // 创建线程 threaddemo02 threaddemo02 = new threaddemo02(); /* * 这里 用了thread的另一个构造方法, * 该构造方法可以传入一个runnable的实现类 * 而我们查看thread的源码可以得知,thread类 原本就实现了runnable * 所里也可以传入一个thread的对象,这样就可以把一个thread对象中的run() * 方法交由其他的线程进行调用 */ thread t1= new thread(threaddemo02); // 启动线程 t1.start(); for (int i = 0; i <10; i++) { system.out.println("main..i:"+i); } system.out.println("main... 主线程结束..."); } }
匿名内部类的方式
system.out.println("-----多线程创建开始-----"); thread thread = new thread(new runnable() { public void run() { for (int i = 0; i< 10; i++) { system.out.println("i:" + i); } } }).start(); system.out.println("-----多线程创建结束-----");
/** * * callbale接口 可以又返回值,可以抛出异常, * 而runnable接口 中的run方法没有返回值,异常只能捕获 * * @author hao * */ public class t002_createwithcallable { public static void main(string[] args) throws interruptedexception, executionexception { mycallable mc = new mycallable(); futuretask<integer> ft = new futuretask<integer>(mc); thread thread = new thread(ft); thread.start(); system.out.println(ft.get()); } } class mycallable implements callable<integer> { public integer call() throws exception { return 124; } }
示例
/* * 什么是守护线程? 守护线程 进程线程(主线程挂了) 守护线程也会被自动销毁. * 该示例中我们手动将子线程设置为守护线程, * 当其他线程(该例中只有主线程)停止时守护线程也会终止 */ public class daemonthread { public static void main(string[] args) { thread thread = new thread(new runnable() { @override public void run() { while (true) { try { thread.sleep(100); } catch (exception e) { // todo: handle exception } system.out.println("我是子线程..."); } } }); thread.setdaemon(true); thread.start(); for (int i = 0; i < 10; i++) { try { thread.sleep(100); } catch (exception e) { } system.out.println("我是主线程"); } system.out.println("主线程执行完毕!"); } }
运行结果
示例代码
/** * yield方法的作用是放弃当前的cpu资源, * 将它让给其他的任务去占用cpu执行时间, * 但是放弃的时间不确定,有可能刚刚放弃,马上又获得cpu时间片 * @author hao * */ public class t009_yield { public static void main(string[] args) { yeildtestthread t1 = new yeildtestthread(); t1.start(); } } class yeildtestthread extends thread{ @override public void run() { super.run(); long begintime = system.currenttimemillis(); int count =0; for(int i =0;i<5000000;i++) { //thread.yield(); count = count +(i+1); } long endtime = system.currenttimemillis(); system.out.println("用时:"+(endtime-begintime)+"毫秒!"); } }
执行结果
注释掉 thread.yield(); 和没注释掉是的执行时间不同。
代码示例
//创建一个线程,子线程执行完毕后,主线程才能执行。 public class t010_join { public static void main(string[] args) throws interruptedexception { thread t1 = new thread(new runnable() { public void run() { for (int i = 0; i < 5; i++) { system.out.println("子线程,i:" + i); } } }); t1.start(); // 当前线程释放资格权,等t1执行完毕之后,才会继续进行执行。 t1.join(); for (int i = 0; i < 5; i++) { system.out.println("main线程,i:" + i); } } }
执行结果
应用场景
有t1、t2、t3三个线程,你怎样保证t2在t1执行完后执行,t3在t2执行完后执行代码如下:
public class jointhreaddemo02 { public static void main(string[] args) { thread t1 = new thread(new runnable() { public void run() { for (int i = 0; i < 20; i++) { system.out.println("t1,i:" + i); } } }); thread t2 = new thread(new runnable() { public void run() { try { t1.join(); } catch (exception e) { } for (int i = 0; i < 20; i++) { system.out.println("t2,i:" + i); } } }); thread t3 = new thread(new runnable() { public void run() { try { t2.join(); } catch (exception e) { } for (int i = 0; i < 20; i++) { system.out.println("t3,i:" + i); } } }); t1.start(); t2.start(); t3.start(); } }
public class t011_priority { public static void main(string[] args) { prioritytthread prioritytthread = new prioritytthread(); thread t1 = new thread(prioritytthread); thread t2 = new thread(prioritytthread); t1.start(); // 注意设置了优先级, 不代表每次都一定会被执行。 只是cpu调度会有限分配 t1.setpriority(10); t2.start(); } } class prioritytthread implements runnable { public void run() { for (int i = 0; i < 100; i++) { system.out.println(thread.currentthread().tostring() + "---i:" + i); } } }
调用interrupt()方法后线程并没有马上停止,仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程。
源码如下
public static boolean interrupted() { return currentthread().isinterrupted(true); } public boolean isinterrupted() { return isinterrupted(false); } /** * tests if some thread has been interrupted. * the interrupted state is reset or not * based on the value of clearinterrupted that is passed. * * 判断某些线程是否已经中断。 * 根据传入的clearinterrupted值来决定是否要重置中断的状态 */ private native boolean isinterrupted(boolean clearinterrupted);
/** * 利用抛出异常的方式来终止线程 * @author hao * */ public class test_exceptioninterrupt { public static void main(string[] args) { try { excepthread t = new excepthread(); t.start(); thread.sleep(2000); t.interrupt(); } catch (interruptedexception e) { system.out.println("main catch"); e.printstacktrace(); } system.out.println("end!"); } } class excepthread extends thread{ @override public void run() { this.stop(); try { for(int i=0;i<500000;i++) { if(thread.interrupted()) { system.out.println("已经是停止状态了,我要退出了!"); // break; throw new interruptedexception(); } system.out.println("i "+(i+1)); } system.out.println("我被输出了 。线程并未停止! 只是for循环被中断了"); } catch (interruptedexception e) { system.out.println("catch t "); e.printstacktrace(); } } }
一个未捕获的异常终止了run方法而使线程猝死。
为了确定线程在当前是否存活着(就是要么是可运行的,要么是被阻塞了),需要使用isalive方法。如果是可运行或被阻塞,这个方法返回true; 如果线程仍旧是new状态且不是可运行的, 或者线程死亡了,则返回false.
如对本文有疑问, 点击进行留言回复!!
浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存
JDK1.6“新“特性Instrumentation之JavaAgent(推荐)
before社区电量是什么意思 Before社区电量获得方法
网友评论