ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:18.33KB ,
资源ID:5762397      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5762397.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java线程知识点总结.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java线程知识点总结.docx

1、java线程知识点总结进程:是一个正在执行的程序,每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个控制单元。线程:就是进程中的一个独立的控制单元,线程在控制着进程的执行。一个进程中至少有一个线程。JVM启动的时候会有一个进程java.exe,该进程至少有一个线程负责java程序的执行,而且这个线程运行的代码存在于main方法中,该线程成为主线程。创建线程的方式:1、继承Thread类步骤:(1)、定义类继承Thread(2)、复写Thread类中的run方法 目的:将自定义的代码存储在run方法中,让线程运行。(3)、调用线程的start方法,该方法有两个作用:启动线程,调用r

2、un方法。2、创建线程的第二种方式:实现Runnable接口 步骤: *(1)、定义类实现Runnable接口 将线程要运行的代码存放在run方法中 (2)、覆盖Runnable接口中的run方法 (3)、通过Thread类建立线程对象 (4)、将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数因为,自定义额run方法所属的对象是Runnable接口的子类对象,所以要让线程去指定对象的run方法,就必须明确run方法所属对象。 (5)、调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。实现方式和继承方式的区别:实现方式好处:避免了单继

3、承的局限性,在定义线程时,建议使用实现方式。继承Thread:线程代码存放在Thread子类run方法中,实现Runnable,线程代码存放在接口的run方法中。发现运行结果每次都不同,因为多个线程都获取cpu的执行权,cpu执行到谁,谁就执行,在某一个时刻,只能有一个程序在运行(多核除外),cpu在做着快速的切换,以达到看上去是同时运行的效果,我们可以形象的把多线程的运行行为看成在互相抢夺cpu的执行权,这就是多线程的一个特性:随机性,谁抢到,谁执行,至于执行多长,cpu说了算。Eg:创建两个线程,和主线程交替执行:class Test extends Thread private Stri

4、ng name; Test(String name) this.name=name; public void run() for(int x=0;x60;x+) System.out.println(name+.run.+x); class ThreadTest public static void main(String args) Test t1=new Test(one); Test t2=new Test(two); t1.start(); t2.start(); for(int x=0;x60;x+) System.out.println(main+x); 五种状态:被创建,运行(s

5、tart(),临时状态、阻塞(具备运行资格,但没有执行权),冻结(放弃了执行资格),消亡(stop()、run()方法执行完成)对象如同锁,持有锁的线程可以在同步中执行;没有持有锁的线程即使获取cpu的执行权,也进不去,因为没有获取锁。Java对于多线程的安全问题提供了专业的解决方式,同步代码块:synchronized(对象) 需要同步的代码;同步的前提:1、必须要有两个或者两个以上的线程2、必须是多个线程使用同一个锁必须保证同步中只能有一个线程在运行。好处:解决多线程的安全问题弊端:多个线程都需要判断锁,较为消耗资源。如何找程序是否有安全问题:1、明确哪些代码是多线程运行代码2、明确共享数

6、据3、明确 多线程运行代码中哪些语句是操作共享数据的。同步函数用的是哪个锁呢?函数需要被对象调用,那么函数都有一个所属对象引用,就是this,所以同步函数用的锁是this。如果同步函数被static修饰后,使用的锁是什么?用过验证,发现不再是this,因为静态方法中不可以定义this,静态进内存时,内存中没有本类 对象,但是一定有该类对应的字节码文件对象,该对象的类型是class,静态的同步方法,使用的锁是该方法所在类的字节码文件对象,类名.class/*线程间通讯:其实就是多个线程在操作同一个资源,但是操作的动作不同。*/class Res private String name; priv

7、ate String sex; boolean flag=false; public synchronized void set(String name,String sex) if(flag) trythis.wait();catch(Exception e) this.name=name; this.sex=sex; flag=true; this.notify(); public synchronized void out() if(!flag) trythis.wait();catch(Exception e) System.out.println(name+.+sex); flag=

8、false; this.notify(); class Input implements Runnable private Res r; Input(Res r) this.r=r; public void run() int x=0; while(true) 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() while(true) r.out(); clas

9、s 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); t1.start(); t2.start(); /new Thread(new Input(r).start();new Thread(new Output(r).start();/将6句变为两句 对于多个生产者和消费者。为什么要定义while判断标记

10、。原因:让被唤醒的线程再一次判断标记。为什么定义notifyAll,因为需要唤醒对方线程。因为只用notify,容易出现只唤醒本方线程的情况。导致程序中的所有线程都等待。实例:生产者消费者问题:class Resource private String name; private int count=1; private boolean flag=false; public synchronized void set(String name) while(flag) trywait();catch(Exception e) this.name=name+-+count+; System.out

11、.println(Thread.currentThread().getName()+生产者+this.name); flag=true; this.notifyAll(); public synchronized void out() while(!flag) trywait();catch(Exception e) System.out.println(Thread.currentThread().getName()+.消费者.+this.name); flag=false; this.notifyAll(); class Producer implements Runnable priva

12、te Resource res; Producer(Resource res) this.res=res; public void run() while(true) res.set(+商品+); class Consumer implements Runnable private Resource res; Consumer(Resource res) this.res=res; public void run() while(true) res.out(); class ProducerConsumerDemo public static void main(String args) Re

13、source 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(); 利用JDK1.5的新特性:import java.util.concurrent.locks.*;clas

14、s ProducerConsumerDemo2 public static void main(String args) Resource r = new Resource(); 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); t1.start(); t2.start(); t3.sta

15、rt(); 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

16、name; private int count = 1; private boolean flag = false; / t1 t2 private Lock lock = new ReentrantLock(); private Condition condition_pro = lock.newCondition(); private Condition condition_con = lock.newCondition(); public void set(String name)throws InterruptedException lock.lock(); try while(fla

17、g) condition_pro.await();/t1,t2 this.name = name+-+count+; System.out.println(Thread.currentThread().getName()+.生产者.+this.name); flag = true; condition_con.signal(); finally lock.unlock();/释放锁的动作一定要执行。 / t3 t4 public void out()throws InterruptedException lock.lock(); try while(!flag) condition_con.a

18、wait(); System.out.println(Thread.currentThread().getName()+.消费者.+this.name); flag = false; condition_pro.signal(); finally lock.unlock(); class Producer implements Runnable private Resource res; Producer(Resource res) this.res = res; public void run() while(true) try res.set(+商品+); catch (Interrupt

19、edException e) class Consumer implements Runnable private Resource res; Consumer(Resource res) this.res = res; public void run() while(true) try res.out(); catch (InterruptedException e) 停止线程:run方法结束,开启多线程运行,运行代码通常是循环结构,只要控制住循环,就可以让run方法结束,也就是线程结束。Thread类中的join()方法,当A线程执行到了B线程的join()方法时,A就会等待,等B线程都执

20、行完,A才会执行,join()可以用来临时加入线程执行。设置优先级:t1.setPriority(Thread.MAX_PRIORITY)yield():暂停正在执行的线程对象,执行其他线程。匿名内部类写多线程同时执行:class ThreadTest public static void main(String args) new Thread() public void run() for(int x=0; x100; x+) System.out.println(Thread.currentThread().getName()+.+x); .start(); for(int x=0; x100; x+) System.out.println(Thread.currentThread().getName()+.+x); Runnable r = new Runnable() public void run() for(int x=0; x100; x+) System.out.println(Thread.currentThread().getName()+.+x); ; new Thread(r).start();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1