java多线程

来源:互联网 发布:有色金属走势软件 编辑:程序博客网 时间:2024/06/05 16:25
java多线程:
1.定义类继承Thread
2.复写Thread类中的run方法
3.调用线程的start方法,
该方法两个作用:启动线程,调用run方法

创建线程的第二种方式:实现Runnable接口


步骤:
1.定义类实现Runnable接口
2.覆盖Runnable接口中run方法
将线程要运行的代码放在run方法中
3.通过Thread类建立线程对象。
4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数
为什么要讲Runnable接口的子类对象传递给Thread的构造函数
因为,自定义的run方法所属的对象是Runnable接口的子类对象。
所以要让线程去指定指定对象的run方法,就必须明确该run方法所属对象

5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法


实现方式和继承方式的区别


实现方式好处:避免了单继承的局限性。
在定义线程时,建议使用实现方式。

继承Thrad:线程代码存放Thread子类run方法中。

实现Runnable,线程代码存放在接口的子类run方法。

class Ticket extends Thread{private int tick = 100;public void run(){while(true){if(tick>0){System.out.println(Thread.currentThread().getName()+"...sale : " + tick--);}}}}class ThreadDemo{public static void main(String[] args){Ticket t = new Ticket();t.start();}}


多线程的运行出现了安全问题
问题原因:当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完。
另一个线程参与进来执行,导致共享数据的错误。


解决办法:
对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中其他线程不可以参与执行。

java对于多线程的安全提供了专业的解决方式。
就是同步代码块

synchronized(对象)
{
需要被同步的代码
}
同步函数使用的对象是this
如果同步函数被静态函数修饰 :静态进内存时,内存中没有本类对象,但是一定有该类对应的字节码文件对象,
类名.class  该对象的类型是Class
同步函数 public synchronized XX()
{
}

对象如同锁,持有锁的线程可以在同步中执行。
没有持有锁的线程及时获取cpu的执行权,也进不去,因为没有获取锁。

同步的前提:
1.必须要有两个或者两个以上的线程。
2.必须是多个线程使用同一个锁

class Ticket implements Runnable//extends Thread{private int tick = 100;public void run(){while(true){synchronized(){if(tick>0){//try(Thread.sleep(10);)catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...sale : " + tick--);}}/*if(tick>0){System.out.println(Thread.currentThread().getName()+"...sale : " + tick--);}*/}}}class ThreadDemo{public static void main(String[] args){Ticket t = new Ticket();Thread t1 = new Thread(t);Thread t2 = new Thread(t);Thread t3 = new Thread(t);Thread t4 = new Thread(t);t1.start();t2.start();t3.start();t4.start();}}


线程等待唤醒机制:
wait(); 等待唤醒机制
notify();
notifyAll();
都使用在同步中,因为要对持有监视器(锁)的线程操作
所以要使用在同步中,因为只有同步才具有锁。

为什么这些操作线程的方法要定义Object类中呢
因为这些方法在操作同步线程时,都必须要标识它们所操作线程只有的锁,
只有同一个锁上的被等待线程,可以被同一个锁上notify唤醒。
不可以对不同锁中的线程惊喜唤醒。

也就是说,等待和唤醒必须是同一个锁。

而锁可以是任意对象,所以可以被任意对象调用的方法定义Object类中。


class Consumer implements Runnable{private Resource r;Consumer(Resource r){this.r = r;}public void run(){while(true){r.out();}}}class Producer implements Runnable{private Resource r;Producer(Resource r){this.r = r;}public void run(){int x = 0;while(true){r.set("商品");}}}class Resource{private String name;private boolean flag=false;private int count=1;public synchronized void set(String name){while(flag)try{this.wait();}catch(Exception e){}this.name = name+"--"+count++;System.out.println(Thread.currentThread().getName()+"...生产..."+this.name);flag = true;this.notifyAll();}public synchronized void out(){while(!flag)try{this.wait();}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...消费........"+this.name);flag = false;this.notifyAll();}}class InputOutputDemo2{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.start();t4.start();}}




0 0
原创粉丝点击