- 浏览: 52709 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
zhoujianboy:
另外一个方法实现eclipse tomcat 热部署:http ...
Eclipse创建Maven Web工程并实现Tomcat热部署
理解线程,首先要明白线程的几种状态,以及状态之间的转换,具体参考下图:
其次,必须理解线程中"锁"的作用,以下引用自sun公司文档Threads and Locks一章中关于Locks的描述:
以上说明,对于多线程来说,正是依靠锁定Object的Monitor来保证同一时间只有一个线程持有某个特定的锁.
Java API中定义Object的wait(),notify()方法就是基于其Monitor来实现:
对于Object Monitor来说,可参考Analyzing Stack Traces文档关于Monitor的部分说明及种类介绍:
多线程下的数据操作需要保证其数据的可靠一致性,为此必须实现线程的同步.以下引用自Intrinsic Locks and Synchronization:
对于以上部分,需要注意以下几点:
1. 线程同步的部分为整个Synchronized同步方法,整个Synchronized同步块,或是Reentrant同步中上锁(lock())与开锁(unlock())之间的部分.
2. 对于多线程synchronized同步块的锁定, 应该限制为锁定同一个Object对象:
synchronized(Object obj){} 注: 如果每个线程锁定的对象不同,则线程之间互不影响,无法达到同步效果.
synchronized同步块还可以直接锁定某个类的所有对象:
synchronized(XXX.class){} 注意没有static synchronized(XXX){}的用法.
锁定对象的线程与锁定类的线程之间互不影响.
3. 对于synchronized同步方法:
①public synchronized void method(){...} 锁定当前对象
②public static synchronized void method(){...} 锁定当前类的所有对象
3.① 等价于public void method(){ synchronized(this){} }
3.② 等价于public void method(){ synchronized(this.class){} }
4. Reentrant同步锁:
线程锁定靠的是Reentrant锁, 依靠调用其lock(),unlock()方法来决定何时何地执行同步.
(1) 与synchronized相比, 使用ReentrantLock(public class ReentrantLock extends Object implements Lock, Serializable),可更加精准的控制线程.
①可以判断线程持有(isHeldByCurrentThread(),isLocked())
②可以用于中断线程(lockInterruptibly())
③设置线程排队的公平性(构造方法)
(2) 专门读写文件, 可使用ReentrantReadWriteLock(public class ReentrantReadWriteLock extends Object implements ReadWriteLock, Serializable)
①该锁专门为多用户读取文件设计,因此封装了读和写之间的关系:允许多用户读(读-读)以及写文件时读(写-读),禁止读文件时写(读-写)以及写文件时再次写(写-写).
②包含两个属性readLock和writeLock, 这两个属性封装好了上述功能, 调用时直接lock(),unlock()即可.
③readLock和writeLock是通过升级(readLock -> writeLock)以及降级(writeLock -> readLock)来完成上述功能, 以下为Java API中的例子:
以下代码为自己练习操作ReentrantReadWriteLock的代码:

其次,必须理解线程中"锁"的作用,以下引用自sun公司文档Threads and Locks一章中关于Locks的描述:
引用
The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.
以上说明,对于多线程来说,正是依靠锁定Object的Monitor来保证同一时间只有一个线程持有某个特定的锁.
Java API中定义Object的wait(),notify()方法就是基于其Monitor来实现:
引用
notify(): Wakes up a single thread that is waiting on this object's monitor.
wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
notifyAll(),wait(long timeout),wait(long timeout, int nanos)...
wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
notifyAll(),wait(long timeout),wait(long timeout, int nanos)...
对于Object Monitor来说,可参考Analyzing Stack Traces文档关于Monitor的部分说明及种类介绍:
引用
A monitor can be thought of as a lock on an object, and every object has a monitor.
...
The following table describes the common registered monitors:
...
The following table describes the common registered monitors:
Monitor | Description | |
utf8 hash table | Locks the hashtable of defined i18N Strings that were loaded from the class constant pool. | |
JNI pinning lock | Protects block copies of arrays to native method code. | |
JNI global reference lock | Locks the global reference table which holds values that need to be explicitly freed, and will outlive the lifetime of the native method call. | |
BinClass lock | Locks access to the loaded and resolved classes list. The global table list of classes | |
Class linking lock | Protects a classes data when loading native libraries to resolve symbolic references | |
System class loader lock | Ensures that only one thread is loading a system class at a time. | |
Code rewrite lock | Protects code when an optimization is attempted. | |
Heap lock | Protects the Java heap during heap memory management | |
Monitor cache lock | Only one thread can have access to the monitor cache at a time this lock ensures the integrity of the monitor cache | |
Dynamic loading lock | Protects Unix green threads JVMs from loading the shared library stub libdl.so more than once at a time. | |
Monitor IO lock | Protects physical I/O for example, open and read. | |
User signal monitor | Controls access to the signal handler if a user signal USRSIG in green threads JVMs. | |
Child death monitor | Controls access to the process wait information when using the runtime system calls to run locals commands in a green threads JVM. | |
I/O Monitor | Controls access to the threads file descriptors for poll/select events | |
Alarm Monitor | Controls access to a clock handler used in green threads JVMs to handle timeouts | |
Thread queue lock | Protects the queue of active threads | |
Monitor registry | Only one thread can have access to the monitor registry at a time this lock ensures the integrity of that registry | |
Has finalization queue lock * | Protects the list of queue lock objects that have been garbage-collected, and deemed to need finalization. They are copied to the Finalize me queue | |
Finalize me queue lock * | Protects a list of objects that can be finalized at leisure | |
Name and type hash table lock * | Protects the JVM hash tables of constants and their types | |
String intern lock * | Locks the hashtable of defined Strings that were loaded from the class constant pool | |
Class loading lock * | Ensures only one thread loads a class at a time | |
Java stack lock * | Protects the free stack segments list |
多线程下的数据操作需要保证其数据的可靠一致性,为此必须实现线程的同步.以下引用自Intrinsic Locks and Synchronization:
引用
Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. (The API specification often refers to this entity simply as a "monitor.") Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.
Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.
When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquistion of the same lock.
Locks In Synchronized Methods
...
Synchronized Statements
...
Reentrant Synchronization
...
Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.
When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquistion of the same lock.
Locks In Synchronized Methods
...
Synchronized Statements
...
Reentrant Synchronization
...
对于以上部分,需要注意以下几点:
1. 线程同步的部分为整个Synchronized同步方法,整个Synchronized同步块,或是Reentrant同步中上锁(lock())与开锁(unlock())之间的部分.
2. 对于多线程synchronized同步块的锁定, 应该限制为锁定同一个Object对象:
synchronized(Object obj){} 注: 如果每个线程锁定的对象不同,则线程之间互不影响,无法达到同步效果.
synchronized同步块还可以直接锁定某个类的所有对象:
synchronized(XXX.class){} 注意没有static synchronized(XXX){}的用法.
锁定对象的线程与锁定类的线程之间互不影响.
3. 对于synchronized同步方法:
①public synchronized void method(){...} 锁定当前对象
②public static synchronized void method(){...} 锁定当前类的所有对象
3.① 等价于public void method(){ synchronized(this){} }
3.② 等价于public void method(){ synchronized(this.class){} }
4. Reentrant同步锁:
线程锁定靠的是Reentrant锁, 依靠调用其lock(),unlock()方法来决定何时何地执行同步.
(1) 与synchronized相比, 使用ReentrantLock(public class ReentrantLock extends Object implements Lock, Serializable),可更加精准的控制线程.
①可以判断线程持有(isHeldByCurrentThread(),isLocked())
②可以用于中断线程(lockInterruptibly())
③设置线程排队的公平性(构造方法)
(2) 专门读写文件, 可使用ReentrantReadWriteLock(public class ReentrantReadWriteLock extends Object implements ReadWriteLock, Serializable)
①该锁专门为多用户读取文件设计,因此封装了读和写之间的关系:允许多用户读(读-读)以及写文件时读(写-读),禁止读文件时写(读-写)以及写文件时再次写(写-写).
②包含两个属性readLock和writeLock, 这两个属性封装好了上述功能, 调用时直接lock(),unlock()即可.
③readLock和writeLock是通过升级(readLock -> writeLock)以及降级(writeLock -> readLock)来完成上述功能, 以下为Java API中的例子:
class CachedData { Object data; volatile boolean cacheValid; ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); void processCachedData() { rwl.readLock().lock(); if (!cacheValid) { // Must release read lock before acquiring write lock rwl.readLock().unlock(); rwl.writeLock().lock(); // Recheck state because another thread might have acquired // write lock and changed state before we did. if (!cacheValid) { data = ... cacheValid = true; } // Downgrade by acquiring read lock before releasing write lock rwl.readLock().lock(); rwl.writeLock().unlock(); // Unlock write, still hold read } use(data); rwl.readLock().unlock(); } }
以下代码为自己练习操作ReentrantReadWriteLock的代码:
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * 对原始数据进行操作的类 * * @author Vincent.zheng */ class Oper { private Map<String, String> sourceMap = new HashMap<String, String>(); private ReadWriteLock lock = new ReentrantReadWriteLock(true); private final Lock readLock = lock.readLock(); private final Lock writeLock = lock.writeLock(); protected Oper() { setSourceMap(); } // 读取单个值 public String read(String key) { try { readLock.lock(); launchPeriod(); return key + ":" + sourceMap.get(key); } finally { readLock.unlock(); } } // public String read() { try { readLock.lock(); launchPeriod(); StringBuffer results = new StringBuffer(); String[] str = new String[sourceMap.size()]; String[] keys = sourceMap.keySet().toArray(str); for (String key : keys) { results.append(key + ":" + sourceMap.get(key) + "; "); } return new String(results); } finally { readLock.unlock(); } } public String write(String key, String value) { try { writeLock.lock(); launchPeriod(); sourceMap.put(key, value); return key + ":" + value; } finally { writeLock.unlock(); } } public String write(Map<String, String> map) { try { writeLock.lock(); launchPeriod(); sourceMap.putAll(map); StringBuffer results = new StringBuffer(); String[] str = new String[map.size()]; String[] keys = map.keySet().toArray(str); for (String key : keys) { results.append(key + ":" + map.get(key) + "; "); } return new String(results); } finally { writeLock.unlock(); } } // 保证每个线程运行时间在5秒以上 private void launchPeriod() { long currentTime = System.currentTimeMillis(); for (;;) { if (System.currentTimeMillis() - currentTime > 5000) { break; } } } // 原始数据 private void setSourceMap() { for (int i = 0; i < 1000; i++) { sourceMap.put("SourceKey" + i, "SourceValue" + i); } } } class Reader extends Thread { public Oper oper; public Reader(String name, Oper oper) { super(name); this.oper = oper; } public void run() { String name = Thread.currentThread().getName(); System.out.println(name + " Start Reading"); // 读全部数据 // String results = oper.read(); // System.out.println(name + " Read=======" + results); // 读单个随机值 String result = oper.read("SourceKey" + new Random().nextInt(1000)); System.out.println(name + " Read=======" + result); } } class Writer extends Thread { public Oper oper; public Writer(String str, Oper oper) { super(str); this.oper = oper; } public void run() { String name = Thread.currentThread().getName(); System.out.println(name + " Start Writing"); // 写全部数据 // String results = oper.write(getWriteData()); // System.out.println(name + " Write=======" + results); // 写单个值 String result = oper.write("WriteSoloKeyIn" + name, "WriteSoloValueIn" + name); System.out.println(name + " Write=======" + result); System.out.println(name + " Read=======" + oper.read()); } // 写入数据 private Map<String, String> getWriteData() { Map<String, String> writeMap = new HashMap<String, String>(); for (int i = 0; i < 10; i++) { writeMap.put("WriteKey" + (i + 1), "WriteValue" + (i + 1)); } return writeMap; } } public class Test { /** * @param args */ public static void main(String[] args) { final Oper oper = new Oper(); ArrayList<Thread> list = new ArrayList<Thread>(); for (int i = 0; i < 100; i++) { Reader reader = new Reader("Reader" + i, oper); list.add(reader); } for (int i = 0; i < 10; i++) { Writer writer = new Writer("Writer" + i, oper); list.add(writer); } ArrayList<Integer> data = new ArrayList<Integer>(); for (int i = 0; i < list.size(); i++) { data.add(i); } for (int i = 0; i < list.size(); i++) { Integer random = new Random(i).nextInt(list.size()); if (data.contains(random)) { data.remove(random); list.get(random).start(); } } } }
相关推荐
3. **原子操作(Atomic Operations)**:原子操作类如AtomicInteger、AtomicLong等,提供了原子性的读写操作,保证了在多线程环境下的数据一致性。这些类通常用于实现非阻塞算法,可以提高并发性能。 4. **同步器...
内容概要:本文详细介绍了基于MATLAB GUI界面和卷积神经网络(CNN)的模糊车牌识别系统。该系统旨在解决现实中车牌因模糊不清导致识别困难的问题。文中阐述了整个流程的关键步骤,包括图像的模糊还原、灰度化、阈值化、边缘检测、孔洞填充、形态学操作、滤波操作、车牌定位、字符分割以及最终的字符识别。通过使用维纳滤波或最小二乘法约束滤波进行模糊还原,再利用CNN的强大特征提取能力完成字符分类。此外,还特别强调了MATLAB GUI界面的设计,使得用户能直观便捷地操作整个系统。 适合人群:对图像处理和深度学习感兴趣的科研人员、高校学生及从事相关领域的工程师。 使用场景及目标:适用于交通管理、智能停车场等领域,用于提升车牌识别的准确性和效率,特别是在面对模糊车牌时的表现。 其他说明:文中提供了部分关键代码片段作为参考,并对实验结果进行了详细的分析,展示了系统在不同环境下的表现情况及其潜在的应用前景。
嵌入式八股文面试题库资料知识宝典-计算机专业试题.zip
嵌入式八股文面试题库资料知识宝典-C and C++ normal interview_3.zip
内容概要:本文深入探讨了一款额定功率为4kW的开关磁阻电机,详细介绍了其性能参数如额定功率、转速、效率、输出转矩和脉动率等。同时,文章还展示了利用RMxprt、Maxwell 2D和3D模型对该电机进行仿真的方法和技术,通过外电路分析进一步研究其电气性能和动态响应特性。最后,文章提供了基于RMxprt模型的MATLAB仿真代码示例,帮助读者理解电机的工作原理及其性能特点。 适合人群:从事电机设计、工业自动化领域的工程师和技术人员,尤其是对开关磁阻电机感兴趣的科研工作者。 使用场景及目标:适用于希望深入了解开关磁阻电机特性和建模技术的研究人员,在新产品开发或现有产品改进时作为参考资料。 其他说明:文中提供的代码示例仅用于演示目的,实际操作时需根据所用软件的具体情况进行适当修改。
少儿编程scratch项目源代码文件案例素材-剑客冲刺.zip
少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip
内容概要:本文详细介绍了基于PID控制器的四象限直流电机速度驱动控制系统仿真模型及其永磁直流电机(PMDC)转速控制模型。首先阐述了PID控制器的工作原理,即通过对系统误差的比例、积分和微分运算来调整电机的驱动信号,从而实现转速的精确控制。接着讨论了如何利用PID控制器使有刷PMDC电机在四个象限中精确跟踪参考速度,并展示了仿真模型在应对快速负载扰动时的有效性和稳定性。最后,提供了Simulink仿真模型和详细的Word模型说明文档,帮助读者理解和调整PID控制器参数,以达到最佳控制效果。 适合人群:从事电力电子与电机控制领域的研究人员和技术人员,尤其是对四象限直流电机速度驱动控制系统感兴趣的读者。 使用场景及目标:适用于需要深入了解和掌握四象限直流电机速度驱动控制系统设计与实现的研究人员和技术人员。目标是在实际项目中能够运用PID控制器实现电机转速的精确控制,并提高系统的稳定性和抗干扰能力。 其他说明:文中引用了多篇相关领域的权威文献,确保了理论依据的可靠性和实用性。此外,提供的Simulink模型和Word文档有助于读者更好地理解和实践所介绍的内容。
嵌入式八股文面试题库资料知识宝典-2013年海康威视校园招聘嵌入式开发笔试题.zip
少儿编程scratch项目源代码文件案例素材-驾驶通关.zip
小区开放对周边道路通行能力影响的研究.pdf
内容概要:本文探讨了冷链物流车辆路径优化问题,特别是如何通过NSGA-2遗传算法和软硬时间窗策略来实现高效、环保和高客户满意度的路径规划。文中介绍了冷链物流的特点及其重要性,提出了软时间窗概念,允许一定的配送时间弹性,同时考虑碳排放成本,以达到绿色物流的目的。此外,还讨论了如何将客户满意度作为路径优化的重要评价标准之一。最后,通过一段简化的Python代码展示了遗传算法的应用。 适合人群:从事物流管理、冷链物流运营的专业人士,以及对遗传算法和路径优化感兴趣的科研人员和技术开发者。 使用场景及目标:适用于冷链物流企业,旨在优化配送路线,降低运营成本,减少碳排放,提升客户满意度。目标是帮助企业实现绿色、高效的物流配送系统。 其他说明:文中提供的代码仅为示意,实际应用需根据具体情况调整参数设置和模型构建。
少儿编程scratch项目源代码文件案例素材-恐怖矿井.zip
内容概要:本文详细介绍了基于STM32F030的无刷电机控制方案,重点在于高压FOC(磁场定向控制)技术和滑膜无感FOC的应用。该方案实现了过载、过欠压、堵转等多种保护机制,并提供了完整的源码、原理图和PCB设计。文中展示了关键代码片段,如滑膜观测器和电流环处理,以及保护机制的具体实现方法。此外,还提到了方案的移植要点和实际测试效果,确保系统的稳定性和高效性。 适合人群:嵌入式系统开发者、电机控制系统工程师、硬件工程师。 使用场景及目标:适用于需要高性能无刷电机控制的应用场景,如工业自动化设备、无人机、电动工具等。目标是提供一种成熟的、经过验证的无刷电机控制方案,帮助开发者快速实现并优化电机控制性能。 其他说明:提供的资料包括详细的原理图、PCB设计文件、源码及测试视频,方便开发者进行学习和应用。
基于有限体积法Godunov格式的管道泄漏检测模型研究.pdf
嵌入式八股文面试题库资料知识宝典-CC++笔试题-深圳有为(2019.2.28)1.zip
少儿编程scratch项目源代码文件案例素材-几何冲刺 V1.5.zip
Android系统开发_Linux内核配置_USB-HID设备模拟_通过root权限将Android设备转换为全功能USB键盘的项目实现_该项目需要内核支持configFS文件系统
C# WPF - LiveCharts Project
少儿编程scratch项目源代码文件案例素材-恐怖叉子 动画.zip