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