Java多线程

来源:互联网 发布:剑灵捏脸数据原图 编辑:程序博客网 时间:2024/05/19 23:17

     线程是进程中一段连续的控制流程或者一段执行路径。它不能够独立存在,必须存在于某个进程之中。线程是一个程序内部的顺序控制流,运行中的程序就是进程,进程是静态的。

    多进程和多线程之间的区别:多线程指的是同一程序中的多个控制流同时执行。

                                           多进程指的是操作系统在同一时间运行多个任务。


        创建线程的两种方法:1、继承Thread类 。   2、实现Runnable接口

     当多线程访问同一个资源的时候会发生不可预测的结果,这时需要用到线程同步来控制。

     线程之间的同步一般是通过synchronized关键字来实现的。

     下面来见一个例子:

     Producer和Consumer都要访问CobbyHole中的content变量。Producer负责向CubbyHole中put内容,而Consumer则在CubbyHole中get内容。通过synchronized关键字修饰put和get、以及wait()、notify()等操作,可以实现每次在Producer在CubbyHole中放置了内容之后,Consumer才在CubbyHole中取内容。避免重复取值和漏取值。

      贴代码:

      CubyHole类:

class CubbyHole {//里面放置的内容private  int content;//定义一个变量决定是否可以取数字,并且初始化为不可取falsepublic boolean available =false;public synchronized int  get() {//如果available为false,即表示不可以取,所以wait()while(available==false){try {wait();} catch (Exception e) {// TODO: handle exception}}//取完数字之后设置为不可取falseavailable=false;//提醒其他的线程wakeupnotifyAll();System.out.println("Consumer "+"got:"+content);return content;}public synchronized void put(int value){//如果available为true则说明可取,也就是说明CubbyHole中的内容还没有被取走//故Producer需要wait到Consumer把其中的数字取走while(available==true){try {wait();} catch (Exception e) {// TODO: handle exception}}//放完之后就变的可取了,所以重新设置为trueavailable=true;content=value;notifyAll();System.out.println("Producer "+"Put:"+content);}}

      Producer类:

public class Producer extends Thread {private CubbyHole cubbyHole;public Producer(CubbyHole c) {cubbyHole=c;}@Overridepublic void run() {for(int i=0;i<10;i++){cubbyHole.put(i);try {sleep((long)Math.random()*10);} catch (Exception e) {System.out.println(e);}}}}

    Consumer类:


public class Consumer extends Thread {private CubbyHole cubbyHole;public Consumer(CubbyHole c) {cubbyHole=c;}@Overridepublic void run() {for(int i=0;i<10;i++){cubbyHole.get();try {sleep((long)Math.random()*10);} catch (Exception e) {System.out.println(e);}}}}

public class Demo {/** * 利用synchronized来实现线程之间的同步 */public static void main(String[] args) {//new一个对象CubbyHole hole=new CubbyHole();//新建两个线程Producer producer=new Producer(hole);Consumer consumer=new Consumer(hole);//启动线程producer.start();consumer.start();}}

运行结果:

Producer Put:0Consumer got:0Producer Put:1Consumer got:1Producer Put:2Consumer got:2Producer Put:3Consumer got:3Producer Put:4Consumer got:4Producer Put:5Consumer got:5Producer Put:6Consumer got:6Producer Put:7Consumer got:7Producer Put:8Consumer got:8Producer Put:9Consumer got:9


    

0 0
原创粉丝点击