- 浏览: 557481 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (340)
- Spring (4)
- Hibernate (2)
- Linux (34)
- Oracle (145)
- Eclipse (1)
- UML (1)
- HTML&&JAVASCRIPT (11)
- JAVA (33)
- 设计模式 (1)
- 版本控制 (1)
- wrap框架 (3)
- IBATIS (5)
- Ruby (1)
- DWR (1)
- MINA (11)
- JBPM (2)
- 缓存技术 (4)
- 网络 (3)
- 应用服务器 (1)
- GWT (5)
- 杂谈 (2)
- ICE (4)
- XML (2)
- ArcGis (2)
- Flex (8)
- junit单元测试 (1)
- SNMP (1)
- 存储 (1)
- office (1)
- MongoDB (0)
- Greenplum (3)
- 管理点滴 (1)
- C++ (6)
- 网络入门 (3)
- Tomcat (7)
- JMX (0)
- webservice (1)
- Oracle的10046事件 (1)
- Library cache内部机制详解 (1)
- expdp通过dblink来导入 (1)
最新评论
-
yuanliangding:
有没有关于mock的更多知识。
基于mock对象和JUnit框架简化Spring Web组件单元测试 -
saup007:
ssh端口不是22,怎么搞呢?
Greenplum 学习笔记 -
springmvc-freemarker:
java开源项目源码实例下载
Apache上全部JAVA开源项目简介 -
bobbell:
哇塞,你真厉害,整理的非常全面。我是一个java barcod ...
Greenplum 学习笔记 -
wsj55133245513324:
这不是bug,你将日志级别从debug提升到INFO 就好了 ...
Spring,smppapi,apache mina, ssl快速实现安全的smpp(5)
A java thread is an execution context or a lightweight process. It is a single sequential flow of control within a program. Programmer may use java thread mechanism to execute multiple tasks at the same time.
What is a Java Thread and How does it work?
Thread class and run() Method
Basic support for threads is in the java.lang.Thread class. It provides a thread API and all the generic behavior for threads. These behaviors include starting, sleeping, running, yielding, and having a priority.
The run() method gives a thread something to do. Its code should implement the thread's running behavior.
There are two ways of creating a customized thread:
Sub classing java.lang.Thread and Overriding run() method.
Implementing the java.lang.Runnable Interface.
Thread Scheduling
When we say that threads are running concurrently, in practice it may not be so. On a computer with single CPU, threads actually run one at a time giving an illusion of concurrency.
The execution of multiple threads on a single CPU based on some algorithm is called thread scheduling.
Thread scheduler maintains a pool of all the ready-to-run threads. Based on fixed priority algorithm, it allocates free CPU to one of these threads.
The Life Cycle of a Thread
The following diagram illustrates the various states that a Java thread can be in at any point during its life and which method calls cause a transition to another state.
Ready-to-run
A thread starts its life cycle with a call to start(). For example
MyThread aThread = new MyThread();
aThread.start();
A call to start() will not immediately start thread's execution but rather will move it to pool of threads waiting for their turn to be picked for execution. The thread scheduler picks one of the ready-to-run threads based on thread priorities.
Running
The thread code is being actively executed by the processor. It runs until it is swapped out, becomes blocked, or voluntarily give up its turn with this static method
Thread.yield();
Please note that yield() is a static method. Even if it is called on any thread object, it causes the currently executing thread to give up the CPU.
Waiting
A call to java.lang.Object's wait() method causes the current thread object to wait. The thread remains in "Waiting" state until some another thread invokes notify() or the notifyAll() method of this object. The current thread must own this object's monitor for calling the wait().
Sleeping
Java thread may be forced to sleep (suspended) for some predefined time.
Thread.sleep(milliseconds);
Thread.sleep(milliseconds, nanoseconds);
Please note that static method sleep() only guarantees that the thread will sleep for predefined time and be running some time after the predefined time has been elapsed.
For example, a call to sleep(60) will cause the currently executing thread to sleep for 60 milliseconds. This thread will be in ready-to-run state after that. It will be in "Running" state only when the scheduler will pick it for execution. Thus we can only say that the thread will run some time after 60 milliseconds.
Blocked on I/O.
A java thread may enter this state while waiting for data from the IO device. The thread will move to Ready-to-Run after I/O condition changes (such as reading a byte of data).
Blocked on Synchronization.
A java thread may enter this state while waiting for object lock. The thread will move to Ready-to-Run when a lock is acquired.
Dead
A java thread may enter this state when it is finished working. It may also enter this state if the thread is terminated by an unrecoverable error condition.
Thread Synchronization
Problems may occur when two threads are trying to access/modify the same object. To prevent such problems, Java uses monitors and the synchronized keyword to control access to an object by a thread.
Monitor
Monitor is any class with synchronized code in it.
Monitor controls its client threads using, wait() and notify() ( or notifyAll() ) methods.
wait() and notify() methods must be called in synchronized code.
Monitor asks client threads to wait if it is unavailable.
Normally a call to wait() is placed in while loop. The condition of while loop generally tests the availability of monitor. After waiting, thread resumes execution from the point it left.
Synchronized code and Locks
Object lock
Each Object has a lock. This lock can be controlled by at most one thread at time. Lock controls the access to the synchronized code.
When an executing thread encounters a synchronized statement, it goes in blocked state and waits until it acquires the object lock. After that, it executes the code block and then releases the lock. While the executing thread owns the lock, no other thread can acquire the lock. Thus the locks and synchronization mechanism ensures proper exceution of code in multiple threading.
Thread Priority
A thread's priority is specified with an integer from 1 (the lowest) to 10 (the highest), Constants Thread.MIN_PRIORITY and Thread.MAX_PRIORITY can also be used. By default, the setPriority() method sets the thread priority to 5, which is the Thread.NORM_PRIORITY.
Thread aThread = Thread.currentThread();
int currentPriority;
currentPriority = aThread.getPriority();
aThread.setPriority( currentPriority + 1 );
Setting priorities may not always have the desired effect because prioritization schemes may be implemented differently on different platforms. However, if you cannot resist messing with priorities, use higher priorities for threads that frequently block (sleeping or waiting for I/O). Use medium to low-priority for CPU-intensive threads to avoid hogging the processor down.
Thread Deadlock
In multiple threading, following problems may occur.
Deadlock or deadly embrace occurs when two or more threads are trying to gain control of the same object, and each one has a lock on another resource that they need in order to proceed.
For example, When thread A waiting for lock on Object P while holding the lock on Object Q and at the same time, thread B holding a lock on Object P and waiting for lock on Object Q, deadlock occurs.
Please note that if the thread is holding a lock and went to a sleeping state, it does not loose the lock. However, when thread goes in blocked state, it normally releases the lock. This eliminates the potential of deadlocking threads.
Java does not provide any mechanisms for detection or control of deadlock situations, so the programmer is responsible for avoiding them
发表评论
-
Apache上全部JAVA开源项目简介
2011-05-12 17:16 1915最近接触Jakarta-Common-BeanU ... -
主题:JAVA NIO 简介
2011-04-15 11:48 9811. 基本 概念 IO 是主存和外部设备 ( 硬盘、终端和 ... -
JNDI在Java EE中的应用
2011-04-01 15:52 1057一、JNDI在Java EE中的应用JNDI技术是Java E ... -
Spring使用Quartz调度器 实现时间点任务(Spring in Action中文版)
2011-03-10 14:08 9977.3.2 使用Quartz调度器 Quartz调度器为调度 ... -
一个分页控件,适合大数据量。可以替换ext的分页控件
2010-11-08 15:44 1116<!doctype html public " ... -
Java的java.nio.Buffer缓冲区基础
2010-10-30 11:02 734缓冲区基础 抽象类Buffer是java.nio包支持缓 ... -
NIO ByteBuffer使用方法
2010-10-30 10:48 1249缓冲区分配和包装 在能够读和写之前,必须有一个缓冲区,用静态方 ... -
将标准的java程序封装为Windows下面的服务
2010-10-19 14:02 13261、首先到请下载最新版本的Java Service Wra ... -
HASH表原理
2010-09-19 16:43 874今天由于天气不好 ... -
使用Apache CXF创建Web Service
2009-12-30 09:30 917官方主页:http://cxf.apache.org/ ... -
java.io.File中的绝对路径和相对路径.
2009-11-12 11:26 4990File类是用来构造文件或文件夹的类,在其构造函数中要求传 ... -
JavaScript极速狂飙:大容量字符型数组的快速检索
2009-06-19 20:19 1100JavaScript 在大容量数组 ... -
谈谈JavaScript中的数组、集合及效率
2009-06-19 20:15 1143数组是JavaScript提供的一个内部对象,它是一个标准的集 ... -
log4jxml配置
2009-05-19 14:45 1119<?xml version="1.0 ... -
log4j日志配置
2009-05-19 11:54 931关键字: apache log4j 1、配置根Logg ... -
bat语法的用法
2009-05-15 14:13 1481首先,批处理文件是 ... -
JUnit学习笔记
2009-05-15 10:15 1442这是我在学习的过程中整理记录下来的,其中参考了网上许多文章 ... -
接口和抽象类的区别
2009-04-30 15:15 1412区别一,两者表达的概念不一样。抽象类是一类事物的高 ... -
如何Gwt中使用Session
2008-12-30 13:54 3171在GWT中可以直接使用session信息,只需要用r ... -
轻松实现Apache,Tomcat集群和负载均衡
2008-07-09 16:54 1055轻松实现Apache,Tomcat集群和负载均衡 ...
相关推荐
8. Explain the concept of encapsulation in OOP and how it is achieved in Java.Answer: Encapsulation is a fundamental principle of Object-Oriented Programming (OOP) that involves bundling data and ...
**Answer**: An abstract class cannot be instantiated directly because it contains one or more abstract methods, and Java does not provide a constructor for abstract classes. When you attempt to create...
That is all it does, and that is also the source of a lot of confusion. Much of what people are familiar with when using POP, like the ability to see how many new mail messages they have, are not ...
It is up to the application to define what consistency means, and isolation in some form is needed to achieve consistent results. SQL Server uses locking to achieve isolation. Definition of ...
To reserve or commit memory and unintentionally not release it when it is no longer being used. A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads...
How Does It Work? Handling Character Types in OLE Applications OLE Containers and the Document/View Architecture Summary Chapter 15: Writing OLE Servers OLE Servers Servers and Containers: ...
A decent rule of thumb is to not inline a function if it is more than 10 lines long. Beware of destructors, which are often longer than they appear because of implicit member- and base-destructor ...
apks are nothing more than a zip file containing resources and compiled java. If you were to simply unzip an apk like so, you would be left with files such as classes.dex and resources.arsc. $ unzip...
Splitting a page when full is simple and does not require a tree traversal for node overflow checking as in a b+tree. Main page list updates are infrequent and hence the locking of the main page list...
if it doesn‘t work I assume that is a host name (someone has a better and simple idea ?).- Added the property FileStream to the class TAtachedFile and the procedure SaveToStream, this was done by ...
Use this with care as this is still somewhat experimental and I'm not sure how useful it is yet. You must make all changes within the buffer sent in to you. Treat the entire file as a stream. Byte ...
Creating a record and accessing its propery is only what you need. Very small memory footprint Last time I checked the dbfDotNet dll was 50Kb. Other databases are 1Mb to 10Mb. I would appreciate if...
There are a few restrictions how the ARM instruction set is implemented. The changes are minor and mostly have a minor impact. For the most part the basic instruction outline is the same. Where ...
2.1.3 What gdb Does During Startup . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Quitting gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 ...
2.1.3 What gdb Does During Startup . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Quitting gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...