Thread Lock

一、 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。

           这样保证每次都线程执行都会先判断条件。一个简单的生产和消费的例子:

Thread states

三、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

	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值