线程

来源:互联网 发布:手机专业制图软件 编辑:程序博客网 时间:2024/06/06 16:51

1、java中要想实现多线程,有两种手段,一种是继承Thread类,另外一种是实现Runable接口。

 继承Thread类

class类名extendsThread{

方法1;

方法2

publicvoidrun(){

// other code…

}

属性1

属性2

 }

直接调用run()方法,按顺序执行。调用start()方法,逐一执行。

 

② 实现Runable接口

class类名implementsRunnable{

方法1;

方法2

publicvoidrun(){

// other code…

}

属性1

属性2

 }

 

Thread中的run方法调用的是Runnable接口的run方法

 

如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。 

 

实现Runnable接口比继承Thread类所具有的优势:

 

1):适合多个相同的程序代码的线程去处理同一个资源

2):可以避免java中的单继承的限制

3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。

java中,每次程序运行至少启动2个线程。一个是main线程,一个是垃圾收集线程。因为每当使用java命令执行一个类的时候,实际上都会启动一个JVM,每一个jVM就是在操作系统中启动了一个进程。 

 

2、线程的休眠

 

try{

                Thread.sleep(2000);

            }catch(Exception e) {

                e.printStackTrace();

            }

3、线程的中断

 hello he =newhello();

        Thread demo =newThread(he,"线程");

        demo.start();

        try{

            Thread.sleep(2000);

        }catch(Exception e) {

            e.printStackTrace();

        }

        demo.interrupt();//2s后中断线程

    }

4、同步和死锁: 

 

【同步代码块】:

语法格式:

synchronized(同步对象){

 //需要同步的代码

}

【同步方法】

也可以采用同步方法。

语法格式为synchronized 方法返回类型方法名(参数列表){

    // 其他代码

}

 

 

 

 

 

当多个线程共享一个资源的时候需要进行同步,但是过多的同步可能导致死锁。

发生死锁的原因一般是两个对象的锁相互等待造成的。

那么为什么会产生死锁呢?
1.因为系统资源不足。2.进程运行推进的顺序不合适。3.资源分配不当。

 

classInfo {

    Public String getName() {

        Return name;

    }

    Public void setName(String name) {

        this.name = name;

    }

    publicintgetAge() {

        Return age;

    }

    Public void setAge(intage) {

        this.age = age;

    }

    Public synchronized void set(String name,intage){

        if(!flag){

            try{

                super.wait();

            }catch(Exception e) {

                e.printStackTrace();

            }

        }

        this.name=name;

        try{

            Thread.sleep(100);

        }catch(Exception e) {

            e.printStackTrace();

        }

        this.age=age;

        flag=false;

        super.notify();

    }

    Public synchronized void get(){

        if(flag){

            try{

                super.wait();

            }catch(Exception e) {

                e.printStackTrace();

            }

        }

        try{

            Thread.sleep(100);

        }catch(Exception e) {

            e.printStackTrace();

        }

        System.out.println(this.getName()+"<===>"+this.getAge());

        flag=true;

        super.notify();

    }

    Private String name ="Rollen";

    Private int age =20;

    Private boolean flag=false;

}

 

/**

 * 生产者

 * */

Class Producer implements Runnable {

    Private Info info =null;

 

    Producer(Info info) {

        this.info = info;

    }

    Public void run() {

        Boolean flag =false;

        for(inti =0; i <25; ++i) {

            if(flag) {

                this.info.set("Rollen",20);

                flag =false;

            }else{

                this.info.set("ChunGe",100);

                flag =true;

            }

        }

    }

}

 

/**

 * 消费者类

 * */

Class Consumer implements Runnable {

    Private Info info =null;

 

    Public Consumer(Info info) {

        this.info = info;

    }

    Public void run() {

        for(inti =0; i <25; ++i) {

            try{

                Thread.sleep(100);

            }catch(Exception e) {

                e.printStackTrace();

            }

            this.info.get();

        }

    }

}

 

/**

 * 测试类

 * */

Class hello {

    Public static void main(String[] args) {

        Info info =newInfo();

        Producer pro =new Producer(info);

        Consumer con =new Consumer(info);

        New Thread(pro).start();

        New Thread(con).start();

    }

}

0 0
原创粉丝点击