Java多线程实例
来源:互联网 发布:零售门店经营数据分析 编辑:程序博客网 时间:2024/04/30 15:41
直接上代码了,解释看注释即可:
package Thread;/* * 参考文章:http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html * */class thread8 implements Runnable{ private int count= 2; public void run() { for(int i=0;i<10;++i) { sale(); } } public synchronized void sale() //对于方法进行同步 { if(count>0) { try { Thread.sleep(2000); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" index:"+ count--); } } }/* 人物信息类 */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) //flag == true代表现在可以set() { try { /*查看p1和p2出现的时间间隔和位置我们可以知道,wait()相当于将线程进行了堵塞,转而执行另一个线程,类似于断点,等另一个线程运行完以后,再继续运行下面的部分 *用专业的话说,就是调用wait()方法,则该线程失去锁,另一线程获取锁,执行完再释放锁,再由该线程捕获,继续下面的操作 * */ System.out.println("p1"); super.wait(); System.out.println("p2"); } catch (Exception e) { e.printStackTrace(); } } this.name=name; try { Thread.sleep(1000); }catch (Exception e) { e.printStackTrace(); } this.age=age; flag=false; super.notify(); /* 唤醒等待中的进程 */ } public synchronized void get() throws InterruptedException { if(flag) //说明现在应该是set()操作 { try{ super.wait(); }catch (Exception e) { e.printStackTrace(); } } try{ Thread.sleep(1000); }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; //标志位进行进程的唤醒和等待,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<6;++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<6;++i) { try { this.info.get(); } catch (InterruptedException e) { e.printStackTrace(); } } }}/* * 消费者 */public class SychronizedThread { public static void main(String[] args) { thread8 trd8=new thread8(); Thread h1=new Thread(trd8); Thread h2=new Thread(trd8); Thread h3=new Thread(trd8); h1.start(); h2.start(); h3.start(); /* 生产者、消费者 */ Info info=new Info(); Producer pro=new Producer(info); Consumer con=new Consumer(info); new Thread(pro).start(); new Thread(con).start(); /* 现在我们对于Info类进行了封装 set() 和 get() ,但是仍然会出现重复读,覆盖之类的问题(即费按照 set() get() 交替的顺序出现) */ /* 为了解决上面出现的问题,我们就需要对于进程进行等待和唤醒 我们只需要修改Info类饥渴,在其中加上标志位,并且通过判断标志位完成等待和唤醒的操作 */ }}
- 【多线程】应用Java多线程实例
- java多线程经典实例
- java多线程经典实例
- java 多线程 实例浅析
- java中的多线程实例
- java多线程爬虫实例
- java多线程实例
- Java多线程实例
- Java多线程实例<转>
- java多线程实现实例
- java 多线程实例二
- java多线程池实例
- java多线程爬虫实例
- Java多线程死锁实例
- java多线程实例
- java多线程实例
- java多线程实例
- java多线程实例
- Cocos2d-x CCScrollView demo
- 用shell脚本写的可以计算出自己来到这个世界的天数
- c++特性:指向类成员的指针和非类型类模板参数
- 利用矩阵快速幂求斐波那契数列
- (Ext基础篇) 弹出窗口
- Java多线程实例
- 大二的一年计划
- 利用dblink实现oracle对oracle数据库的同步
- eclipse的Android sdk manager刷不出API列表 解决办法
- JAVA中使用JSON进行数据传递
- 64位win7下PL/SQL Developer连接oracle 出错ORA-12154: TNS:无法解析指定的连接标识符
- 利用jQuery对无序列表排序
- HDU 3460 Ancient Printer
- Spring之AOP