




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
下面是我原来在CSDN论坛上看到的一个贴子,涉及到同步,wait(),notify()等概念的理解,我试着根据原来的一些回复和Think in Java上的相关概念将wait()和notify()这两个方法剖析了一下,欢迎指教.问题如下:file:/分析这段程序,并解释一下,着重讲讲synchronized、wait(),notify 谢谢!class ThreadA public static void main(String args) ThreadB b=new ThreadB(); b.start(); System.out.println(b is start.); synchronized(b)/括号里的b是什么意思,起什么作用? try System.out.println(Waiting for b to complete.); b.wait();/这一句是什么意思,究竟让谁wait? System.out.println(Completed.Now back to main thread); catch (InterruptedException e) System.out.println(Total is :+b.total); class ThreadB extends Thread int total; public void run() synchronized(this) System.out.println(ThreadB is running.); for (int i=0;i a2.lockOrder() / 确定并设置对象的锁定 / 顺序。 first = a2; last = a1; synchronized(first) / 按正确的顺序锁定对象。 synchronized(last) int arr1 = a1.array(); int arr2 = a2.array(); for (int i=0; i value += arr1i + arr2i; return value; public void run() /. 对于4,如果线程进入,则线程在该类中所有操作不能进行,包括静态变量和静态方法,实际上,对于含有静态方法和静态变量的代码块的同步,我们通常用4来加锁.以上4种之间的关系: 锁是和对象相关联的,每个对象有一把锁,为了执行synchronized语句,线程必须能够获得synchronized语句中表达式指定的对象的锁,一个对象只有一把锁,被一个线程获得之后它就不再拥有这把锁,线程在执行完synchronized语句后,将获得锁交还给对象。 在方法前面加上synchronized修饰符即可以将一个方法声明为同步化方法。同步化方法在执行之前获得一个锁。如果这是一个类方法,那么获得的锁是和声明方法的类相关的Class类对象的锁。如果这是一个实例方法,那么此锁是this对象的锁。 下面谈一谈一些常用的方法: wait(),wait(long),notify(),notifyAll()等方法是当前类的实例方法, wait()是使持有对象锁的线程释放锁; wait(long)是使持有对象锁的线程释放锁时间为long(毫秒)后,再次获得锁,wait()和wait(0)等价; notify()是唤醒一个正在等待该对象锁的线程,如果等待的线程不止一个,那么被唤醒的线程由jvm确定; notifyAll是唤醒所有正在等待该对象锁的线程. 在这里我也重申一下,我们应该优先使用notifyAll()方法,因为唤醒所有线程比唤醒一个线程更容易让jvm找到最适合被唤醒的线程. 对于上述方法,只有在当前线程中才能使用,否则报运行时错误java.lang.IllegalMonitorStateException: current thread not owner. 下面,我谈一下synchronized和wait()、notify()等的关系:1.有synchronized的地方不一定有wait,notify2.有wait,notify的地方必有synchronized.这是因为wait和notify不是属于线程类,而是每一个对象都具有的方法,而且,这两个方法都和对象锁有关,有锁的地方,必有synchronized。另外,请注意一点:如果要把notify和wait方法放在一起用的话,必须先调用notify后调用wait,因为如果调用完wait,该线程就已经不是current thread了。如下例:/* * Title: Jdevelopers Java Projdect * Description: n/a * Copyright: Copyright (c) 2001 * Company: soho http:/www.ChinaJavaW * * version 1.0 */import java.lang.Runnable;import java.lang.Thread;public class DemoThread implements Runnable public DemoThread() TestThread testthread1 = new TestThread(this, 1); TestThread testthread2 = new TestThread(this, 2); testthread2.start(); testthread1.start(); public static void main(String args) DemoThread demoThread1 = new DemoThread(); public void run() TestThread t = (TestThread) Thread.currentThread(); try if (!t.getName().equalsIgnoreCase(1) synchronized (this) wait(); while (true) System.out.println(time in thread + t.getName() + = + t.increaseTime(); if (t.getTime() % 10 = 0) synchronized (this) System.out.println(*); notify(); if (t.getTime() = 100) break; wait(); catch (Exception e) e.printStackTrace(); class TestThread extends Thread private int time = 0; public TestThread(Runnable r, String name) super(r, name); public int getTime() return time; public int increaseTime() return+time; 下面我们用生产者/消费者这个例子来说明他们之间的关系: public class test public static void main(String args) Semaphore s = new Semaphore(1); Thread t1 = new Thread(s, producer1); Thread t2 = new Thread(s, producer2); Thread t3 = new Thread(s, producer3); Thread t4 = new Thread(s, consumer1); Thread t5 = new Thread(s, consumer2); Thread t6 = new Thread(s, consumer3); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); class Semaphore implements Runnable private int count; public Semaphore(int n) this.count = n; public synchronized void acquire() while (count = 0) try wait(); catch (InterruptedException e) /keep trying count-; public synchronized void release() while (count = 10) try wait(); catch (InterruptedException e) /keep trying count+; notifyAll(); /alert a thread thats blocking on this semaphore public void run() while (true) if (Thread.currentThread().getName().substring(0,8).equalsIgnoreCase(consumer) acquire(); else if (Thread.currentThread().getName().substring(0,8).equalsIgnoreCase(producer) release(); System.out.println(Thread.currentThread().getName() + + count); 生产
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业无人机租赁市场2025年用户需求变化趋势与服务平台运营应对
- 工程-发包方案-降幅(3篇)
- 电气工程方案落实(3篇)
- 犬和蛇咬伤课件
- 牧场食堂安全培训课件
- 安全教育安全培训课件
- 林业国企面试题库及答案
- 科技服务业信用评价规范
- 涟水语文面试题库及答案
- 劳动活动面试题库及答案
- 白介素2联合地塞米松治疗气道过敏性炎症性疾病:疗效与机制的深度剖析
- 2025年江苏省农业融资担保有限责任公司招聘笔试参考题库附带答案详解
- 口腔护理论文-口腔论文-临床医学论文-医学论文
- 部队油库承包合同协议
- 江苏语文单招试题及答案
- 2024第41届全国中学生物理竞赛预赛试题(含答案)
- 直播分成合同协议
- 2025年(幼儿园)教师资格考试《保教知识与能力》模拟测试题及答案(共三套)
- 物业管理服务项目进度保证措施
- 土石方水利工程资质挂靠协议
- 文化体育中心(文化馆)建设项目可行性研究报告
评论
0/150
提交评论