一、 Create a thread
A thread can be created in two forms:extend thread or complement runnable interface.
(1) Extend Thread in 3 step:
1 Define a thread by extending a thread class,override run() method
2 Instantiate your thread object by new.
3 Start your thread by executint start() method of your thread.
In the next case,no new thread will be created.The method run() will be executed in th main thread.It means System.out.println("Main is running"); will certainly appear at the end of body. out.println("Main is running"); only can be executed after the control returned from run() method.
Two reminds:
If you want run() method to be executed in a separate thread,do invoke it directly.Just invoke it by calling start() method.
If you call run() method directly,it will be executed in the calling thread,not in its own thread.
(2) Create a thread by implementing a runnable interface in 3 steps
1 Implement runnable interface and override run() method.
2 Create your class instance which class implements runnable interface
3 Intantiate a new thread object whose constructor use your class instance as its arguement.
4 invoke start() method of the new thread object.
二、Liftcycle of Thread
Main states of the thread:
1 New:A thread is already instantiated,not yet started.
2 Runnable:In the runnable state,the thread is ready to run and waiting to be selected by scheduler for running.
A thread entered the Runnable state first time when the start() method is invoked by the thread instance.
The thread can come back to runnable state from Nonrunnable state.
3 Running:It's the state in which the thread is running
4 Nonrunnable state:
Blocked:The thread is in blocked state when it's waiting for the resource such as I/O or an object's lock.
Sleeping:The Thread stays in sleeping state for a specific time.
Calling sleep() method in run() method body of the specific thread will turn this running thread into nonrunnable state from running state.
Waiting:The thread goes into the waiting state when the object which the thread running in invoke the wait() method.The call to notify() and notifyAll() method (called by another thread)will bring the thread back to runnable state.
wait(),notify(),notifyAll is implemented in object class,not in thread class.They can only be called in a synchronized piece of code.
如果不符合线程running条件的时候,就revoke wait() method.如果符合running条件,就会执行wait()语句下面的code.wait()和notify()组合起来用的顺序如下:
1 先判断线程是否不符合执行的条件(比如生产线程会判断生产的产品是否已经过量count>6)。如果超过了容量了,就让线程休眠,等其他也需要获得这个对象锁的线程(消费线程)来notify()这个线程。
2 如果符合运行的条件,如果没有超过容量,线程continue producing,执行wait()语句下面的code。
这样保证每次都线程执行都会先判断条件。一个简单的生产和消费的例子:
三、Transition between states of the thread
(1) Running ---> Runnable(Thread.yield()).
It must be called as the following example:
Thread.yield();
A call to yield() method in a thread code puts the thread back into runnable state,in order to allow other thread to share CPU time.About yield() method,one of the following will happend:
If no other threads are waiting for CPU time,the scheduler will put this thread back into running state immediately
If there are other threads waiting for CPU,the thread may have to wait for its turn before it can be put back into running.
(2)Runnable---->Unrunnable
blocked
waiting:
1 To synchronize a few things,you will put a thread into waiting state by a call to wait() method in a shared code.A thread in waiting state is brought out of this state by a call to notify() or notifyAll methods
2 Sometimes.You need to keep waiting until some other threads are completed.The code block the current thread from becoming runnable until the thread completes,which the current thread refer to.
In other words ,the join method join the current thread to the end of the end of the main thread which the current thread refers to.
sleeping:A thread is put into sleeping state by a call to sleep() method in the thread code.The sleep(.....) like the yield(....) method,is static method of the thread class.
五、Synchronization and locks
Becuase we have multiple thread running in the same programe, these thread may have to access the shared resource.To make sure to access the shared resources concurrenctly,the threads should be running in concurrency.
(1) object locks
Only if the instance is sington between the concurrent threads,the object locks can work in synchronized way.
Thread t1 and Thread t2 are sharing the same AppleBox instance.So the Synchronized function,synchronized(this) code segments can work in concurrent way.
com.citibank.aimy.Example.server.RemoteServiceImpl
com.citibank.aimy.Example.client.RMIClient
set path=C:/JavaLib/jdkb1590/bin
cd C:/TestJava/axis2+spring/Basic
rmic -keep com.citibank.aimy.Example.server.RemoteServiceImpl
rmiregistry
Djava.security.policy=client.policy
wait() 必须在synchronized code里面,有wait才有notify()。Thread.Sleep在任何方法中都可以用。
package com.leozhou;
public class Semaphore {
private int resource = 0 ;
public Semaphore(int resource){
this.resource = resource;
}
private Object obj = new Object();
public void request() throws InterruptedException{
while(true){
synchronized(obj){
if(this.resource >0){
this.resource --;
return;
}
}
Thread.sleep(10L);
}
}
public void release(){
synchronized(obj){
this.resource ++;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}