JAVA线程知识点总结

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro); Thread t2 = new Thread(pro); Thread t3 = new Thread(con); Thread t4 = new Thread(con);
if(flag) try{this.wait();}catch(Exception e){}
=name; this.sex=sex; flag=true; this.notify(); } public synchronized void out() { if(!flag)
try{this.wait();}catch(Exception e){} System.out.println(name+"...."+sex); flag=false; this.notify(); } } class Input implements Runnable { private Res r; Input(Res r) { this.r=r; } public void run() { int x=0; while(true) {
while(true) {
r.out(); } }
} class InputOutputDemo {
public static void main(String[] args) {
Res r=new Res(); Input in=new Input(r); Output out=new Output(r); Thread t1=new Thread(in); Thread t2=new Thread(out);
if(x==0) r.set("mike","man");
else r.set("丽丽","女"); x=(x+1)%2; } }
} class Output implements Runnable {
private Res r; Output(Res r) {
this.r=r; } public void run() {
进程:是一个正在执行的程序,每一个进程执行都有一个执行顺序,该顺序是一个执行路径, 或者叫一个控制单元。 线程:就是进程中的一个独立的控制单元,线程在控制着进程的执行。 一个进程中至少有一个线程。 JVM 启动的时候会有一个进程 java.exe,该进程至少有一个线程负责 java 程序的执行,而且 这个线程运行的代码存在于 main 方法中,该线程成为主线程。 创建线程的方式: 1、 继承 Thread 类
res.out(); } }
} class ProducerConsumerDemo {
public static void main(String[] args) {
Resource res=new Resource(); Producer pro=new Producer(res); Consumer con=new Consumer(res); Thread t1=new Thread(pro); Thread t2=new Thread(pro); Thread t3=new Thread(con); Thread t4=new Thread(con); t1.start(); t2.start(); t3.start(); t4.start();
try{wait();}catch(Exception e){} } System.out.println(Thread.currentThread().getName()+"...消费者..."+); flag=false; this.notifyAll(); } } class Producer implements Runnable { private Resource res; Producer(Resource res) { this.res=res; } public void run() { while(true) {
为什么定义 notifyAll,
因为需要唤醒对方线程。 因为只用 notify,容易出现只唤醒本方线程的情况。导致程序中的所有线程都等待。 实例:生产者消费者问题: class Resource {
private String name; private int count=1; private boolean flag=false; public synchronized void set(String name) {
t1.start(); t2.start(); t3.start(); t4.start();
} }
/* JDK1.5 中提供了多线程升级解决方案。 将同步 synchronized 替换成现实 Lock 操作。 将 Object 中的 wait,notify notifyAll,替换了 Condition 对象。 该对象可以 Lock 锁 进行获取。 该示例中,实现了本方只唤醒对方操作。
Lock:替代了 Synchronized lock unlock newCondition()
Condition:替代了 Object wait notify notifyAll await(); signal(); signalAll();
*/ class Resource {
private String name; private int count = 1; private boolean flag = false;
发现运行结果每次都不同,因为多个线程都获取 cpu 的执行权,cpu 执行到谁,谁就执行, 在某一个时刻,只能有一个程序在运行(多核除外),cpu 在做着快速的切换,以达到看上 去是同时运行的效果,我们可以形象的把多线程的运行行为看成在互相抢夺 cpu 的执行权, 这就是多线程的一个特性:随机性,谁抢到,谁执行,至于执行多长,cpu 说了算。 Eg:创建两个线程,和主线程交替执行: class Test extends Thread {
public static void main(String[] args) {
Test t1=new Test("one"); Test t2=new Test("two"); t1.start(); t2.start(); for(int x=0;x<60;x++) {
System.out.println("main"+x); } } } 五种状态:被创建,运行(start()),临时状态、阻塞(具备运行资格,但没有执行权), 冻结(放弃了执行资格),消亡(stop()、run()方法执行完成) 对象如同锁,持有锁的线程可以在同步中执行;没有持有锁的线程即使获取 cpu 的执行权, 也进不去,因为没有获取锁。 Java 对于多线程的安全问题提供了专业的解决方式,同步代码块: synchronized(对象) { 需要同步的代码; } 同步的前提: 1、 必须要有两个或者两个以上的线程 2、 必须是多个线程使用同一个锁 必须保证同步中只能有一个线程在运行。 好处:解决多线程的安全问题 弊端:多个线程都需要判断锁,较为消耗资源。 如何找程序是否有安全问题: 1、 明确哪些代码是多线程运行代码 2、 明确共享数据 3、 明确 多线程运行代码中哪些语句是操作共享数据的。 同步函数用的是哪个锁呢? 函数需要被对象调用,那么函数都有一个所属对象引用,就是 this,所以同步函数用的锁是 this。 如果同步函数被 static 修饰后,使用的锁是什么? 用过验证,发现不再是 this,因为静态方法中不可以定义 this,静态进内存时,内存中没有 本类 对象,但是一定有该类对应的字节码文件对象,该对象的类型是 class,静态的同步方
将线程要运行的代码存放在 run 方法中 (2)、覆盖 Runnable 接口中的 run 方法 (3)、通过 Thread 类建立线程对象 (4)、将 Runnable 接口的子类对象作为实际参数传递给 Thread 类的构造函数
因为,自定义额 run 方法所属的对象是 Runnable 接口的子类对象,所以要让 线程去指定对象的 run 方法,就必须明确 run 方法所属对象。 (5)、调用 Thread 类的 start 方法开启线程并调用 Runnable 接口子类的 run 方法。 实现方式和继承方式的区别: 实现方式好处:避免了单继承的局限性,在定义线程时,建议使用实现方式。 继承 Thread:线程代码存放在 Thread 子类 run 方法中,实现 Runnable,线程代码存放在接 口的 run 方法中。
步骤: (1)、定义类继承 Thread (2)、复写 Thread 类中的 run 方法
目的:将自定义的代码存储在 run 方法中,让线程运行。 (3)、调用线程的 start 方法,该方法有两个作用:启动线程,调用 run 方法。 2、创建线程的第二种方式:实现 Runnable 接口
步骤: *(1)、定义类实现 Runnable 接口
// t1 t2 private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition(); private Condition condition_con = lock.newCondition();
t1.start(); t2.start(); //new Thread(new Input(r)).start();new Thread(new Output(r)).start();//将 6 句变为两句 } } 对于多个生产者和消费者。 为什么要定义 while 判断标记。 原因:让被唤醒的线程再一次判断标记。
while(flag) try{wait();}catch(Exception e){}
=name+"--"+count++; System.out.println(Thread.currentThread().getName()+"生产者"+); flag=true; this.notifyAll(); } public synchronized void out() { while(!flag) {
res.set("+商品+"); }
}}
class Consumer implements Runnable {
private Resource res; Consumer(Resource res) {
this.res=res; } public void run() {
while(true) {
法,使用的锁是该方法所在类的字节码文件对象,类名.class /* 线程间通讯: 其实就是多个线程在操作同一个资源, 但是操作的动作不同。
*/ class Res {
private String name; private String sex; boolean flag=false; public synchronized void set(String name,String sex) {
private String name; Test(String name) {
=name; } public void run() {
for(int x=0;x<60;x++) {
System.out.println(name+"..run.."+x); } }
} class ThreadTest {
} } 利用 JDK1.5 的新特性: import java.util.concurrent.locks.*;
class ProducerConsumerDemo2 {
public static void main(String[] args) {
Resource r = new Resource();
相关文档
最新文档