博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java多线程编程
阅读量:6805 次
发布时间:2019-06-26

本文共 8238 字,大约阅读时间需要 27 分钟。

hot3.png

Java多线程相关的两个类:

  1. Thread,需要继承
  2. Runnable,需要实现这个接口

相同点: 1.都要实现run方法,调用时使用start方法。 2.Thread也是实现Runnable

不同点: 1.Thread不适合资源共享 class hello extends Thread { public void run() { for (int i = 0; i < 7; i++) { if (count > 0) { System.out.println("count= " + count--); } } }

public static void main(String[] args) {        hello h1 = new hello();        hello h2 = new hello();        hello h3 = new hello();        h1.start();        h2.start();        h3.start();    }     private int count = 5;}

输出时,count不能够共享 2. Runnable可以实现共享

class MyThread implements Runnable{        private int ticket = 5;  //5张票             public void run() {            for (int i=0; i<=20; i++) {                if (this.ticket > 0) {                    System.out.println(Thread.currentThread().getName()+ "正在卖票"+this.ticket--);                }            }        }    }public class lzwCode {        public static void main(String [] args) {            MyThread my = new MyThread();            new Thread(my, "1号窗口").start();            new Thread(my, "2号窗口").start();            new Thread(my, "3号窗口").start();        }    }

此时,输出结果为: ![在此输入图片描述][1] [1]: 3. 线程同步 共享资源变量时,需要资源同步 同步时,分为同步对象与同步方法两种情形: 3.1 同步对象 synchronized(同步对象){ //需要同步的代码 } 如买票问题: class hello implements Runnable { public void run() { for(int i=0;i<10;++i){ synchronized (this) { if(count>0){ try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(count--); } } } }

public static void main(String[] args) {        hello he=new hello();        Thread h1=new Thread(he);        Thread h2=new Thread(he);        Thread h3=new Thread(he);        h1.start();        h2.start();        h3.start();    }    private int count=5;}

3.2 方法同步: 同如买票问题: class hello implements Runnable { public void run() { for (int i = 0; i < 10; ++i) { sale(); } }

public synchronized void sale() {        if (count > 0) {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(count--);        }    }     public static void main(String[] args) {        hello he = new hello();        Thread h1 = new Thread(he);        Thread h2 = new Thread(he);        Thread h3 = new Thread(he);        h1.start();        h2.start();        h3.start();    }     private int count = 5;}

3.3生产者和消费者问题 示例代码: class Info {

public String getName() {        return name;    }     public void setName(String name) {        this.name = name;    }     public int getAge() {        return age;    }     public void setAge(int age) {        this.age = age;    }     private String name = "Rollen";    private int age = 20;} class Producer implements Runnable{    private Info info=null;    Producer(Info info){        this.info=info;    }         public void run(){        boolean flag=false;        for(int i=0;i<25;++i){            if(flag){                this.info.setName("Rollen");                try{                    Thread.sleep(100);                }catch (Exception e) {                    e.printStackTrace();                }                this.info.setAge(20);                flag=false;            }else{                this.info.setName("chunGe");                try{                    Thread.sleep(100);                }catch (Exception e) {                    e.printStackTrace();                }                this.info.setAge(100);                flag=true;            }        }    }}class Consumer implements Runnable{    private Info info=null;    public Consumer(Info info){        this.info=info;    }         public void run(){        for(int i=0;i<25;++i){            try{                Thread.sleep(100);            }catch (Exception e) {                e.printStackTrace();            }            System.out.println(this.info.getName()+"<---->"+this.info.getAge());        }    }} class hello{    public static void main(String[] args) {        Info info=new Info();        Producer pro=new Producer(info);        Consumer con=new Consumer(info);        new Thread(pro).start();        new Thread(con).start();    }}

问题:错位; 解决办法:添加同步; class Info {

public String getName() {        return name;    }     public void setName(String name) {        this.name = name;    }     public int getAge() {        return age;    }     public void setAge(int age) {        this.age = age;    }     public synchronized void set(String name, int age){        this.name=name;        try{            Thread.sleep(100);        }catch (Exception e) {            e.printStackTrace();        }        this.age=age;    }         public synchronized void get(){        try{            Thread.sleep(100);        }catch (Exception e) {            e.printStackTrace();        }        System.out.println(this.getName()+"<===>"+this.getAge());    }    private String name = "Rollen";    private int age = 20;} class Producer implements Runnable {    private Info info = null;     Producer(Info info) {        this.info = info;    }     public void run() {        boolean flag = false;        for (int i = 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 (int i = 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 = new Info();        Producer pro = new Producer(info);        Consumer con = new Consumer(info);        new Thread(pro).start();        new Thread(con).start();    }}

问题:出现重复读,重复写; 解决办法:加入等待和通知; class Info {

public String getName() {        return name;    }     public void setName(String name) {        this.name = name;    }     public int getAge() {        return age;    }     public void setAge(int age) {        this.age = age;    }     public synchronized void set(String name, int age){        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 (int i = 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 (int i = 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 = new Info();        Producer pro = new Producer(info);        Consumer con = new Consumer(info);        new Thread(pro).start();        new Thread(con).start();    }}

转载于:https://my.oschina.net/u/250670/blog/271978

你可能感兴趣的文章
How to install snmpwalk snmpget on CentOS 6.4 6.3 5.9 Redhat RHEL Fedora
查看>>
最小生成树
查看>>
Mybatis中配置Mapper的方法
查看>>
Java基础学习总结(19)——Java环境变量配置
查看>>
Mvc5+Entity Framework6 之二----在MVC中用Entity Framework实现基本的CRUD
查看>>
我的友情链接
查看>>
大型网站技术架构(四)网站的高性能架构
查看>>
linux系统修改SSH最大连接数,修改nofile,nproc参数方法
查看>>
Hadoop-2.5.2集群安装配置详解
查看>>
解决报表网页版转成excel时,首位0被清除的问题
查看>>
Mysql学习总结(3)——MySql语句大全:创建、授权、查询、修改等
查看>>
MyBatis学习总结(8)——Mybatis3.x与Spring4.x整合
查看>>
Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解...
查看>>
IIS 7.0 和 IIS 7.5 中的 HTTP 状态代码
查看>>
Dubbo学习总结(1)——Dubbo入门基础与实例讲解
查看>>
rsync搭建及管理
查看>>
STL:std::shared_ptr大致原理.
查看>>
高并发学习笔记(八)
查看>>
第四章 项目管理一般知识
查看>>
Python 调用cobbler API 学习笔记
查看>>