• 线程概述:

    • 多线程的目的,不是提高程序的执行速度,而是提高程序的使用率(能抢到CPU的可能比较大). 因为线程是CPU调度的基本单位,所以,当一个程序的线程较多的时候就更容易抢到cpu的资源
    • 进程:
      • 运行中的程序,是系统进行资源分配和调度的独立单位
      • 每个进程都有他自己的内存空间和系统资源
    • 线程:
      • 是进程中的单个顺序控制流,是一条执行路径,是cpu调度的基本单位
      • 一个进程如果有多条执行路径,称:多线程
      • 一个进程如果只有一条执行路径,称:单线程
  • 线程生命周期:
  • java中线程概览: [ ps : 图来自郭朝:http://blog.csdn.net/smartbetter/article/details/51335165]
  • 继承Thread类 和 实现Runnable:
 /**
*继承Thread类 和 实现Runnable:
* 1.后者解决了java单继承的局限性
2.后者更适合多个相同的程序代码处理同一个资源的情况,将数据与代码有效分离,体现了java面向对象的思维
*
*有人说:
* 前者是多个线程各自执行各自的任务,
* 后者是多个线程处理相同的任务
*
*一般用后者的人居多,因为更灵活
*/ // 继承Thread类
class MyThread extends Thread{
private static Object shareData; // 实现数据共享
@Overwrite
public void run(){
System.out.println("当前线程是"+Thread.currentThread.getName());
}
}
// 创建线程
class Main{
public static void main(String[] args) {
Thread thread=new MyThread();
Thread thread2=new MyThread();
thread.start(); // 线程开启
thread2.start(); // 线程开启
}
}
  • 实现Runnable创建线程类:

  

 // 实现Runnable创建线程类
class MyRunnable implements Runnable{
private Object shareData; // 实现数据共享
public void run(){
System.out.println("当前线程是"+Thread.currentThread.getName());
}
}
//创建线程
class Main{
public static void main(String[] args) {
// 两个线程将共享shareData共享数据
Runnable runa=new MyRunnable();
Thread thread=new MyThread(runa);
Thread thread2=new MyThread(runa);
thread.start(); // 线程开启
thread2.start(); // 线程开启
}
}
  • 使用Callable 和 Future接口:

  

 /**
* 该代码来源: @蛊惑Into
* Callable 和 Future接口
* Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。
* Callable和Runnable有几点不同:
* (1)Callable规定的方法是call(),而Runnable规定的方法是run().
* (2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。
* (3)call()方法可抛出异常,而run()方法是不能抛出异常的。
* (4)运行Callable任务可拿到一个Future对象,
* Future 表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。
* 通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。
*/
public class CallableAndFuture { public static class MyCallable implements Callable{
private int flag = 0;
public MyCallable(int flag){
this.flag = flag;
}
public String call() throws Exception{
if (this.flag == 0){
return "flag = 0";
}
if (this.flag == 1){
try {
while (true) {
System.out.println("looping.");
Thread.sleep(2000);
}
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
return "false";
} else {
throw new Exception("Bad flag value!");
}
}
} public static void main(String[] args) { // 定义3个Callable类型的任务
MyCallable task1 = new MyCallable(0);
MyCallable task2 = new MyCallable(1);
MyCallable task3 = new MyCallable(2); // 创建一个执行任务的服务
ExecutorService es = Executors.newFixedThreadPool(3);
try {
// 提交并执行任务,任务启动时返回了一个Future对象, // 如果想得到任务执行的结果或者是异常可对这个Future对象进行操作
Future future1 = es.submit(task1);
// 获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行
System.out.println("task1: " + future1.get()); Future future2 = es.submit(task2);
// 等待5秒后,再停止第二个任务。因为第二个任务进行的是无限循环
Thread.sleep(5000);
System.out.println("task2 cancel: " + future2.cancel(true)); // 获取第三个任务的输出,因为执行第三个任务会引起异常
// 所以下面的语句将引起异常的抛出
Future future3 = es.submit(task3);
System.out.println("task3: " + future3.get());
} catch (Exception e){
System.out.println(e.toString());
} // 停止任务执行服务
es.shutdownNow(); } }
  • 线程控制:

  

 // 线程控制

 join()线程---必须等线程th执行完成后才能继续向下执行
...
th.start();
th.join();
... yield()礼让---注意和线程通信中wait()区分,yield将线程放到就绪队列,而wait将其放到阻塞队列
...th.doSomething...
th.yield();
...th.doSomethingElse... sleep()睡眠---线程睡眠,将进入阻塞队列
...doSomething...
Thread.sleep(1000);
...continueDoSomething... setDaemon()后台线程---将线程设置为后台线程,将在前台线程都死亡后自动死亡
....
th.setDaemon(true);
.... setPrority()优先级---设置优先级
...
th.setPrority(5);
th.start();
...
  • 线程同步的3种实现:

  

 /* 线程同步的3种实现:
* 1.同步代码块:锁对象可以是任意对象
synchronized(obj){同步代码块}
* 2.同步方法:
2.1 普通同步方法,锁对象是this
public synchronized void fun(){...}
2.2 静态同步方法,锁对象是.class字节码对象
public static synchronized void fun(){...}
3.同步锁:javaAPI提供了多种锁,如读写锁等,以及一些工具类的锁(同时具有同步和通讯特点)
private Lock lock=new ReentrantLock();
...
lock.lock();
...doSomething...
lock.unLock();
*
*
*以下着重介绍几种工具类的锁使用:
Samaphore
CyclicBarrier
CountDownLatch
*/
/**
* 线程互斥与通信:
* 线程通讯之传统线程通讯: Semaphore信号灯工具
* 传统的互斥,是由线程本身来控制锁的占有和释放,这就导致必须当前线程完成,或是主动让出锁,下一个线程才有机会,
* 而semaphore工具, 提供相当于专门的锁的管理方全权控制索,任何线程的请求都通过管理方的许可,这样就解放了锁的控制权,让多个线程共享锁
*
*共有3盏灯,一次来了10个人,只用其中的3个人能够拿到等,当有灯还回的时候,等候队列的人才有机会拿到灯
*可用于死锁恢复的一些场景
*
* @throws Exception
* @author ware E-mail:
* @version create time: 20172017年3月1日下午11:35:41
*/
public class ThreadCommunication3 { public static void main(String[] args) {
final Semaphore sp=new Semaphore(3, true); ExecutorService pool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) {
Runnable task=new Runnable() {
@Override
public void run() {
try {
sp.acquire(); // 请求锁
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程"+Thread.currentThread().getName()+"已经进入---可用数"+(3-sp.availablePermits())); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("线程"+Thread.currentThread().getName()+"准备离开"); sp.release(); //释放锁
}
}; pool.submit(task);
} } } /**
* 线程互斥与通信:
* 线程通讯之传统线程通讯: CyclicBarrier工具
* 所有规定的线程必须都在完成某一项工作之后才能继续下一项工作
*
* 如: 班级郊游,只有所有的人都到了集合点(工作1)才能出发(工作2)
*
* @throws Exception
* @author ware E-mail:
* @version create time: 20172017年3月1日下午11:35:41
*/
public class ThreadCommunication4 { public static void main(String[] args) {
final CyclicBarrier cb=new CyclicBarrier(4); // 约定有多少个人 ExecutorService pool = Executors.newCachedThreadPool(); for (int i = 0; i < 4; i++) {
Runnable task=new Runnable() {
@Override
public void run() { try {
// 第一阶段工作
Thread.sleep((long) (Math.random()*1000));
System.out.println(Thread.currentThread().getName()+"到达集合点,当前共有"+cb.getNumberWaiting()+"人在等待");
cb.await(); // 第二阶段工作
Thread.sleep((long) (Math.random()*1000));
System.out.println(Thread.currentThread().getName()+"出发中"+cb.getNumberWaiting()+"人出发中");
cb.await(); // 第三阶段工作
Thread.sleep((long) (Math.random()*1000));
System.out.println(Thread.currentThread().getName()+"到达景点"+cb.getNumberWaiting()+"人到达景点");
cb.await(); } catch (Exception e) {
e.printStackTrace();
} } }; pool.submit(task);
} } } /**
* 线程互斥与通信:
* 线程通讯之传统线程通讯: CountDownLatch工具
* 如同一个倒计时器,当countDown()到0的时候,所有等该"计时器"的线程都会受到消息
*
* 如: 所有的运动员都在等裁判的命令,裁判一声令下,所有的运动员收到消息开始跑,裁判等到所有的人都跑到终点了才宣布结构
*
* @throws Exception
* @author ware E-mail:
* @version create time: 20172017年3月1日下午11:35:41
*/
public class ThreadCommunication6 { public static void main(String[] args) {
final CountDownLatch cdOrder=new CountDownLatch(1); // 裁判命令
final CountDownLatch cdAnswer=new CountDownLatch(4); // 运动员的消息状态 ExecutorService pool = Executors.newCachedThreadPool(); // 4个运动员
for (int i = 0; i < 4; i++) {
Runnable task=new Runnable() {
@Override
public void run() { try {
System.out.println(Thread.currentThread().getName()+"---已经准备好了,等待命令"); cdOrder.await(); // 等命令 System.out.println("等到命令并开跑");
Thread.sleep((long) (Math.random()*1000));
System.out.println(Thread.currentThread().getName()+"跑完全程了"); cdAnswer.countDown(); } catch (Exception e) {
e.printStackTrace();
} } }; pool.submit(task);
} // 1个裁判
Runnable coach=new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()+"准备开炮.....碰...");
cdOrder.countDown();
System.out.println(Thread.currentThread().getName()+"命令已发出,等待结果..."); cdAnswer.await();
System.out.println(Thread.currentThread().getName()+"比赛结束,成绩出来了"); } catch (InterruptedException e) {
e.printStackTrace();
} }
};
pool.submit(coach); } }
  • 线程通讯:

  

 /*
*3种线程通讯:
1. 传统线程通讯--wait(),notify();
2. Condition线程通讯;
2. BlockingQueue阻塞队列
*
*/
/**
* 线程互斥与通信:
* 线程通讯之传统线程通讯: wait(),notify(),notifyAll()(必须配合synchronized关键字)
*
*父线程执行10次,
*子线程执行50次
* @author ware E-mail:
* @version create time: 20172017年3月1日下午11:35:41
*/
public class ThreadCommunication {
private static boolean isSub=false; public synchronized void doit(){
while(true){
while(isSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("main"+"----------"+i);
}
isSub=true;
this.notify();
} }
public synchronized void doit2(){
while(true){
while(!isSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < 50; i++) {
System.out.println("sub"+"----------"+i);
}
isSub=false;
this.notify();
}
} public static void main(String[] args) {
ThreadCommunication tc=new ThreadCommunication(); // 父线程
Thread fatherTh=new Thread(){
@Override
public void run() {
tc.doit();
}
}; // 子线程
Thread sonTh=new Thread(){
@Override
public void run() {
tc.doit2();
}
}; fatherTh.start();
sonTh.start();
}
} /**
* 线程互斥与通信:
* 线程通讯之传统线程通讯: Condition(必须配合Lock使用) * 是对阻塞队列的实现: ArrayBlockQueue
* @throws Exception
* @author ware E-mail:
* @version create time: 20172017年3月1日下午11:35:41
*/
public class ThreadCommunication2 { private static final Lock lock=new ReentrantLock();
private static final Condition notFull=lock.newCondition();
private static final Condition notEmpty=lock.newCondition(); private Object resources[]; int putIndex; // 下一个可取的资源索引
int takeIndex; // 下一个可以存放资源的索引
int count; // 当前总资源数 public ThreadCommunication2(int resourceLimit) {
resources=new Object[resourceLimit];
this.putIndex=0;
this.takeIndex=0;
this.count=0;
} public void put(Object taskResource){
lock.lock();
try {
while(count==resources.length){
notFull.await();
}
System.out.println("-----------------put");
resources[putIndex]=taskResource;
putIndex=(putIndex%resources.length);
count++;
notEmpty.signalAll();
} catch (Exception e) {
e.printStackTrace();
}finally{
lock.unlock();
}
} public Object take(){
Object result=null;
lock.lock();
try {
while(count==0){
notEmpty.await();
}
System.out.println("----------------take");
result=resources[takeIndex];
takeIndex=(takeIndex+1)%resources.length;
count--; notFull.signalAll();
} catch (Exception e) {
e.printStackTrace();
}finally{
lock.unlock();
} return result;
} public static void main(String[] args) {
ThreadCommunication2 tc=new ThreadCommunication2(7); // 资源队列的长度为7 // 开10个放线程
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
tc.put(Thread.currentThread().getName());
}
}).start();
} // 开10个拿线程
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(tc.take());
}
}).start();
}
} } /**
*BlockingQueue阻塞队列
*放线程在队列满的时候直接放弃,通知拿线程,然后进入阻塞队列,
*拿线程在队列空的时候直接放弃,通知放线程,然后进入阻塞队列,
*/
public class DequeueTest { private final static BlockingQueue<Object> queue=new ArrayBlockingQueue<>(5); public static void main(String[] args) throws InterruptedException { // 开10个放线程
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
queue.add(Thread.currentThread().getName());
}
}).start();
} // 开10个拿线程
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(queue.remove());
}
}).start();
} } }
  • 线程数据共享:

  

 /* 线程数据共享:
* 1.多个线程间共享同一个数据:
1.1.多个线程的执行代码相同---多个线程共用一个Runnable的实例, 那么Runnable中的类成员将被共享;
* 1.2.多个线程的执行代码不同---为多个Runnable对象传入同一个实体对象
* 1.3.多个线程的执行代码不同---将多个Runnable设置为内部类,将共享实体设置为外部类成员变量
* 2.一个线程的多个代码块共享一个数据:
* 2.1 将 (线程,数据对象) 放到一个map中,
* 2.2 一般使用java类库提供的ThreadLocal,实现原理同上,但是更优雅,而且会在线程结束后自动清理
*
* 线程间的数据共享比较简单,这里着重介绍线程内的数据共享
*/
/**
* 线程范围内的共享:多个模块在同一个线程中运行时要共享一份数据
*
* 解决方式: 使用ThreadLocal---优雅版---将线程与数据分离
*
* @param
* @return
* @throws Exception
* @author ware E-mail:
* @version create time: 20172017年3月2日上午9:07:16
*/
public class ThreadScopeShared4 {
// private static ThreadLocal<Integer> local=new ThreadLocal<>(); public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
// int data=new Random().nextInt();
// local.set(data); ThreadScopeSharedData.getInstance().setAge(new Random().nextInt());
ThreadScopeSharedData.getInstance().setName("vivi");
new A().getNum();
new B().getNum();
}
}).start();
} } static class A{ // 模块A
public void getNum(){
int age=ThreadScopeSharedData.getInstance().getAge();
String name=ThreadScopeSharedData.getInstance().getName();
System.out.println(Thread.currentThread().getName()+"---------"+age);
}
}
static class B{ // 模块B
public void getNum(){
int age=ThreadScopeSharedData.getInstance().getAge();
String name=ThreadScopeSharedData.getInstance().getName();
System.out.println(Thread.currentThread().getName()+"---------"+age);
}
}
} // 专门与线程绑定的对象
class ThreadScopeSharedData{
private ThreadScopeSharedData() {}
public static ThreadScopeSharedData getInstance(){
ThreadScopeSharedData instance=map.get();
if(instance==null){
instance=new ThreadScopeSharedData();
map.set(instance);
}
return instance;
} private static ThreadLocal<ThreadScopeSharedData> map=new ThreadLocal<>(); private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin-top:0cm;
mso-para-margin-right:0cm;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0cm;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}

最新文章

  1. ubuntu:activate root
  2. Centos 内存占满 释放内存
  3. 2016C#模拟谷歌Google登陆Gmail&amp;Youtube小案例
  4. 项目中使用Quartz集群分享--转载
  5. 使用Yeoman搭建 AngularJS 应用 (3) —— 让我们搭建一个网页应用
  6. 《ASP.NET 本质论》HttpApplication的处理管道 ,HttpMoudle,HttpHandler
  7. 简单的cocos2d-x手势(转)
  8. 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表
  9. hdu-4302-Holedox Eating-线段树-单点更新,有策略的单点查询
  10. 在Linux中设置共享目录
  11. php传输大数据大文件时候php.ini相关设置
  12. Java异常处理-----自行处理
  13. id为194024的进程当前未运行
  14. python win32com.client
  15. SQL Server连接查询之Cross Apply和Outer Apply的区别及用法(转载)
  16. golang ffmpeg 做网络直播
  17. SQL Server 事件探查器和数据库引擎优化顾问
  18. windows无法访问linux服务器
  19. Sequelize-nodejs-11-Raw queries
  20. vue 开发过程中遇到的问题

热门文章

  1. tomcat 修改网站路径(Java之负基础实战)
  2. PHP之Mysql常用SQL语句示例的深入分析
  3. PHP做负载均衡回话保持问题参考
  4. 滚动视图(ScrollView)的功能与用法
  5. ImageView及其子类(三)
  6. BootstrapTable(附源码) Bootstrap结合BootstrapTable的使用,分为两种模试显示列表。
  7. mybatis springmvc调用oracle存储过程,返回记录集
  8. 通过浏览器navigator判断浏览器版本或者手机类型&amp;&amp;判断微信访问
  9. Linux笔记(十四) - 日志管理
  10. Dubbo源码学习--集群负载均衡算法的实现