跳至内容
wiki
用户工具
登录
站点工具
工具
显示页面
修订记录
反向链接
最近更改
媒体管理器
网站地图
登录
最近更改
媒体管理器
网站地图
您的足迹:
分享:技术:多线程:多线程编程核心技术整理
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== 多线程编程核心技术整理 ====== ===== 前言 ===== 2016年5月12日东哥在微信读书APP上送给我一本书《JAVA多线程编程核心技术》,作者高洪岩。在接下来的7天内读完了,感觉这本书写的挺好,作者基本把所有的知识点都结合代码例子来讲解,容易被程序员接受。其实在读的过程当中,就想着要把这本书的知识点做个整理,用于备忘也用于更好的掌握多线程。可能在我尝试整理知识点的时候,某些我觉得比较不常用的,我会比较简单的整理。 ===== 多线程基础 ===== ==== 进程和多线程的概念及线程的优点 ==== * 进程 比如WINDOWS任务管理器里,看到的QQ.EXE,就是一个进程 * 线程 比如QQ中的视频,发送文件,发送文字图片等,都是独立在运作的“线程” * 线程的优点 多线程或者说多任务操作系统,可以最大限度地利用CPU的空闲时间在不同的任务之间不停的切换,由于切换的速度非常快,使使用者感觉多个任务是在同时进行的。当然后来也出现多个CPU,或者一个CPU多核就是为了加快机器运算。 ==== 使用多线程 ==== === 打印主线程的名字 === <code java Test.java> package com.gxx.threads.study.test1; /** * 测试类-打印主线程的名字 * @author Gxx */ public class Test { /** * main方法 * @param args */ public static void main(String[] args) { /** * 打印main方法当前线程名字 * 输出:main */ System.out.println(Thread.currentThread().getName()); } } </code> 输出 <code> main </code> === 两种实现线程类的方法 === * 继承Thread实现线程类 <code java MyThread1.java> package com.gxx.threads.study.test2; /** * 继承Thread实现线程类 * @author Gxx */ public class MyThread1 extends Thread { /** * 覆盖Thread中的run方法 */ @Override public void run() { System.out.println("MyThread1.run()"); } } </code> * 实现Runnable实现线程类 <code java MyThread2.java> package com.gxx.threads.study.test2; /** * 实现Runnable实现线程类 * @author Gxx */ public class MyThread2 implements Runnable { /** * 实现Runnable中的run方法 */ @Override public void run() { System.out.println("MyThread2.run()"); } } </code> * 两种实现的区别 本质上并没有什么区别,但是由于JAVA只支持单继承,如果继承了Thread就不能继承其它的父类了,所以绝大多数情况下是由于这个,而Thread类其实也实现了Runnable接口。 === 测试start()、run()和多次start() === * start() start()通过“线程规划器”此线程已经准备就绪,等待调用run(),CPU在不确定的时间调用run() * run() 直接执行run,就不是“线程规划器”来调度了,而是执行run()的当前线程直接调用方法,如果是main()中调用,则当前线程就是main * 多次start() 多次start()会抛出异常java.lang.IllegalThreadStateException,报线程状态异常 <code java MyThread.java> package com.gxx.threads.study.test3; /** * 自定义线程类 * @author Gxx */ public class MyThread extends Thread { /** * 覆盖run方法 */ @Override public void run() { System.out.println("线程[" + Thread.currentThread().getName() + "]执行"); } } </code> <code java Test.java> package com.gxx.threads.study.test3; /** * 测试类-测试start和run和多次start * @author Gxx */ public class Test { /** * main方法 * @param args */ public static void main(String[] args) { MyThread thread = new MyThread(); thread.run(); thread.start(); thread.start(); } } </code> 输出 <code> 线程[main]执行 线程[Thread-0]执行 Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:705) at com.gxx.threads.study.test3.Test.main(Test.java:16) </code> === 不共享变量,线程安全 === <code java MyThread.java> package com.gxx.threads.study.test3; /** * 自定义线程类 * @author Gxx */ public class MyThread extends Thread { /** * 计数 */ int count = 10; /** * 覆盖run方法 */ @Override public void run() { System.out.println("线程[" + Thread.currentThread().getName() + "]执行count=" + (count--)); } } </code> <code java TestSafe.java> package com.gxx.threads.study.test3; /** * 测试类-不共享变量,线程安全 * @author Gxx */ public class TestSafe { /** * main方法 * @param args */ public static void main(String[] args) { MyThread thead1 = new MyThread(); MyThread thead2 = new MyThread(); MyThread thead3 = new MyThread(); MyThread thead4 = new MyThread(); MyThread thead5 = new MyThread(); MyThread thead6 = new MyThread(); MyThread thead7 = new MyThread(); MyThread thead8 = new MyThread(); MyThread thead9 = new MyThread(); MyThread thead10 = new MyThread(); thead1.start(); thead2.start(); thead3.start(); thead4.start(); thead5.start(); thead6.start(); thead7.start(); thead8.start(); thead9.start(); thead10.start(); } } </code> 输出 <code> 线程[Thread-0]执行count=10 线程[Thread-2]执行count=10 线程[Thread-1]执行count=10 线程[Thread-3]执行count=10 线程[Thread-4]执行count=10 线程[Thread-5]执行count=10 线程[Thread-6]执行count=10 线程[Thread-7]执行count=10 线程[Thread-8]执行count=10 线程[Thread-9]执行count=10 </code> === 共享变量,线程不安全 === <code java TestNotSafe.java> package com.gxx.threads.study.test3; /** * 测试类-共享变量,线程不安全 * @author Gxx */ public class TestNotSafe { /** * main方法 * @param args */ public static void main(String[] args) { MyThread thread = new MyThread(); Thread thead1 = new Thread(thread); Thread thead2 = new Thread(thread); Thread thead3 = new Thread(thread); Thread thead4 = new Thread(thread); Thread thead5 = new Thread(thread); Thread thead6 = new Thread(thread); Thread thead7 = new Thread(thread); Thread thead8 = new Thread(thread); Thread thead9 = new Thread(thread); Thread thead10 = new Thread(thread); thead1.start(); thead2.start(); thead3.start(); thead4.start(); thead5.start(); thead6.start(); thead7.start(); thead8.start(); thead9.start(); thead10.start(); } } </code> 输出 <code> 线程[Thread-2]执行count=10 线程[Thread-3]执行count=9 线程[Thread-1]执行count=10 线程[Thread-4]执行count=8 线程[Thread-5]执行count=7 线程[Thread-6]执行count=6 线程[Thread-7]执行count=5 线程[Thread-8]执行count=4 线程[Thread-9]执行count=3 线程[Thread-10]执行count=2 </code> === 共享变量,使用synchronized使得线程安全 === <code java Tools.java> package com.gxx.threads.study.test4; /** * 工具类 * @author Gxx */ public class Tools { /** * 计数 */ int count = 10; /** * 打印count * @throws Exception */ public synchronized void printCount() { try { count--; /** * 睡眠1秒 */ Thread.sleep(1000); System.out.println("线程[" + Thread.currentThread().getName() + "]执行count=" + count); } catch (InterruptedException e) { e.printStackTrace(); } } } </code> <code java MyThread.java> package com.gxx.threads.study.test4; /** * 自定义线程类 * @author Gxx */ public class MyThread extends Thread { /** * 工具类 */ Tools tools; /** * 构造方法 * @param tools */ public MyThread(Tools tools){ this.tools = tools; } /** * 覆盖父类的run方法 */ @Override public void run() { tools.printCount(); } } </code> <code java Test.java> package com.gxx.threads.study.test4; /** * 测试类-共享变量,使用synchronized使得线程安全 * @author Gxx */ public class Test { /** * main方法 * @param args */ public static void main(String[] args) { Tools tools = new Tools(); /** * 共用同一个tools */ MyThread thread1 = new MyThread(tools); MyThread thread2 = new MyThread(tools); thread1.start(); thread2.start(); } } </code> 输出 <code> 线程[Thread-0]执行count=9 线程[Thread-1]执行count=8 </code> === count--和System.out.println() === 注意: * count--不是同步的,分三步1.获取count的值;2.count-1;3.再赋值给count;不同步则有时间差 * System.out.println()是同步的 <code java println.java> public void println(String x) { synchronized (this) { print(x); newLine(); } } </code>
分享/技术/多线程/多线程编程核心技术整理.1464489812.txt.gz
· 最后更改: 2016/05/29 10:43 由
gxx
页面工具
显示页面
修订记录
反向链接
回到顶部