java基础学习总结—— 线程
2017-02-08 21:18
561 查看
首先什么是进程?什么是线程?
一些都是linux方面的知识,我想到java这也是一样的吧。
程序的实例就是“进程”,一个程序,同时执行多次,则产生不同的进程,程序是静态,进程是动态的。线程是进程内部的一个控制序列,当你创建一个线程,就像家里多了一个家庭成员。当你去创建进程,那就是创建一个家庭。进程的结构
进程的组成:程序代码,数据,变量,文件描述符,环境组成。进程都有自己的“栈空间”“程序计数器”“变量”。
同一个程序有多个进程
线程
线程是一个程序内部的顺序控制流。 线程和进程的区别: 1.每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有教程的开销。 2.线程可以看成是轻量级的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器(PC),线程切换的开销小。 3.多进程:在操作系统中能同时运行多个任务(程序)。 4.多线程:在同一个应用程序中有多个顺序流通时执行。 多线程的优势: 编程简单,效率高(能直接共享数据和资源,多进程不能),适用于开发服务程序(如web服务,聊天服务等)。
创建线程
Thread的常用方法
线程控制的基本方法图
线程优先级
sleep方法的介绍
重写run方法是应该注意:
线程的让步:
线程的合并,也叫串行化:
线程的同步:
synchronized修饰方法时,其他线程将不能锁定当前线程已经锁定的this对象
nodify方法和wait方法的小例子:
补充sleep方法与wait方法的区别
public class TT implements Runnable { int b = 100; public synchronized void m1() throws Exception{ b = 1000; System.out.println("t.start线程锁定当前对象并开始陷入睡眠"); Thread.sleep(5000); System.out.println("t.start线程---->b = " + b); System.out.println("t.start线程即将释放对对象的锁定"); } public synchronized void m2() throws Exception { System.out.println("主线程锁住当前对象并开始陷入睡眠"); Thread.sleep(2500); b = 2000; System.out.println("主线程即将释放对对象的锁定,此时的b="+b); } public void run() { try { System.out.println("t.start线程开始"); m1(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { TT tt = new TT(); Thread t = new Thread(tt); t.start(); tt.m2(); System.out.println("主线程中输出b:"+tt.b); } }
两次运行,有可能得到的两个不同的结果
死锁
public class TestDeadLock implements Runnable { public int flag = 1; static Object o1 = new Object(), o2 = new Object(); public void run() { System.out.println("flag=" + flag); if(flag == 1) { synchronized(o1) { System.out.println("o1对象被线程1锁住!"); try { Thread.sleep(500);//放大结果 } catch (Exception e) { e.printStackTrace(); } synchronized(o2) { System.out.println("线程1要拿到对对象o2的锁定,执行完此语句方法才能释放对o1对象的锁定!"); } } } if(flag == 0) { synchronized(o2) { System.out.println("o2对象被线程2锁住!"); try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } synchronized(o1) { System.out.println("线程2要拿到对对象o1的锁定,执行完此语句方法才能释放对o2对象的锁定!"); } } } } public static void main(String[] args) { TestDeadLock td1 = new TestDeadLock(); TestDeadLock td2 = new TestDeadLock(); td1.flag = 1; td2.flag = 0; Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); t1.start(); t2.start(); } }
cmd下死锁结果图
生产消费的问题
问题:生产馒头的人不断的往篮子里面放入生产出来的馒头,吃货则不断的从篮子里面那馒头填肚子。分析:
先确定 生产者 消费者 馒头 装馒头的篮子 这几个对象类。对馒头类,每个窝头加个标记id,对篮子类来说,有 往里面生产 和 往外面消费 这两个方法、对生产者以及消费者这两个线程类来说,都要拿到篮子这个类对象那个的引用,而后重写各自的run方法即可。
这里的重点就是 两个线程的同步,记住 synchronized修饰方法时,其他线程将不能锁定当前线程已经锁定的this对象
public class ProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start(); // new Thread(p).start(); // new Thread(p).start(); new Thread(c).start(); } } class WoTou { int id; WoTou(int id) { this.id = id; } public String toString() { return "WoTou : " + id; } } class SyncStack { int index = 0; WoTou[] arrWT = new WoTou[6]; public synchronized void push(WoTou wt) { System.out.println("生产线程锁定当前对象,调用push方法开始生产了!" ); while(index == arrWT.length) { try { System.out.println("生产线程因为馒头个数封顶而将陷入等待!" ); this.wait(); System.out.println("生产线程被唤醒!" ); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); arrWT[index] = wt; index ++; System.out.println("生产线程已经生产一个!" ); } public synchronized WoTou pop() { System.out.println("消费线程锁定当前对象,调用pop方法开始消费了!" ); while(index == 0) { try { System.out.println("消费线程因为馒头个数为零而将陷入等待!" ); this.wait(); System.out.println("消费线程被唤醒!" ); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); index--; System.out.println("消费线程已经消费一个!" ); return arrWT[index]; } } class Producer implements Runnable { SyncStack ss = null; Producer(SyncStack ss) { this.ss = ss; } public void run() { System.out.println("生产线程开始生产!"); for(int i=0; i<20; i++) { WoTou wt = new WoTou(i); ss.push(wt); System.out.println("生产线程刚生产了馒头:" + wt); try { Thread.sleep((int)(Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { SyncStack ss = null; Consumer(SyncStack ss) { this.ss = ss; } public void run() { System.out.println("消费线程开始消费!"); for(int i=0; i<20; i++) { WoTou wt = ss.pop(); System.out.println("消费刚吃掉了馒头: " + wt); try { Thread.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
原文链接:http://sheer.iteye.com/blog/1889539
相关文章推荐
- java基础学习总结——线程(二)
- Java基础学习总结(88)——线程创建与终止、互斥、通信、本地变量
- Java基础学习总结(88)——线程创建与终止、互斥、通信、本地变量
- java线程之基础学习总结(二)
- Java多线程基础学习之线程的创建方式总结
- java基础学习总结——线程(一)
- java基础学习总结——线程(一)
- java线程之基础学习总结(三)
- Java基础学习总结(68)——有关Java线程方面的面试题
- java基础学习总结-----线程(一)
- Java基础学习总结(68)——有关Java线程方面的面试题
- Java基础学习笔记(十)线程的创建总结
- java基础学习总结——线程(一)
- java基础学习总结——线程(一)
- Java基础学习总结(94)——Java线程再学习
- Java基础学习总结(94)——Java线程再学习
- java基础学习总结——线程之使用Runnable接口创建线程的方法
- java基础学习总结——线程(二)