difference of wait ,sleep.

本文详细解析了Java中线程的状态变化,包括调用sleep、yield和wait方法时线程的状态转换及其对锁的影响。此外,还介绍了wait方法的三种形式及其实现线程间通信的具体机制。
当调用sleep ,yield 的时候,并不释放所拥有的锁,
而调用wait的时候,则释放所拥有的锁,
Thread.sleep(long milliseconds): sends the current thread into Non-Runnable state for the specified amount of time. But, this doesn’t cause the thread to loose ownership of the acquired monitors. So, if the current thread is into a synchronized block/method, then no other thread will be able to enter that block/method. This method throws ‘InterruptedException’ if another thread interrupts it. There is another variant of the ‘sleep()’ method as well where it accepts two arguments – one, long milliseconds, and second, int nanoseconds. Evidently, it causes the current thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds. The second argument ‘int nanoseconds’ can acquire a value of the range 0-999999. Another noticeable difference between the two variants is that sleep(long milliseconds, int nanoseconds) throws an IllegalArgumentException if either the value of milliseconds is negative or the value of nanoseconds is not in the range 0-999999. As is the case with the first variant, it throws InterruptedException is another thread interrupts it. Common Error: both these methods are static, so even if you try to call ‘t.sleep(…)’ on a different thread, it’ll be called on the current thread only and not on the ‘t’ thread. In fact, one should avoid calling any static method on an object reference. wait() method: There are three variants of this method in the ‘Object’ class:- public final void wait(long timeout) public final void wait(long timeout, int nanoseconds) public final void wait() All the three methods throw InterruptedException & IllegalMonitorStateException. The first two may also throw IllegalArgumentException. The wait() method also sends the current thread into Non-Runnable state like the sleep() method. But, the difference between the two is that in case of ‘wait()’ the locks are released before going into Non-Runnable state. Another apparent difference is that ‘wait()’ is an instance method, while sleep() is a static method. The method ‘wait()’ should be called for an object only when the current thread has already acquired lock for that object. This causes the current thread to wait either another thread invokes the ‘notify()’ method or the ‘notifyAll()’ method for this object, or a specified amount of time has elapsed and after that the thread starts participating in thread scheduling process to acquire the monitor of the object to proceed further. The wait() method causes the current thread to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. After the execution of this method invocation, the thread becomes disabled for any scheduling purposes and lies dormant until one of the following things happen:- Any other thread invokes ‘notify()’ method this object and the thread under consideration is arbitrarily chosen as the thread to be awakened. Any other thread invokes ‘notifyAll()’ for this object. Any other thread interrupts the thread under consideration. The specified amount of time has elapsed (in case first two variants of wait() are used) After any of the four above mentioned events happens, the thread is removed from the wait set for this object and re-enabled for thread scheduling. It’ll compete for the rights to synchronize on the object in an usual manner and it’ll keep doing this until it acquires control on the object and gains all the synchronization claims on the object, which it had acquired at the time of ‘wait()’ invocation. This means that after the return from wait() method, the synchronization state of object and of the thread will be exactly the same as it was before the wait() invocation.
def run_aging_chamber(): try: if target_temperature is not None and target_humidity is not None: # get current temperature and humidity current_temperature_data = GWSChamberCtrl.get_temperature() current_humidity_data = GWSChamberCtrl.get_humidity() current_temperature = float(GWSChamberCtrl.get_current_temperature(current_temperature_data)) current_humidity = float(GWSChamberCtrl.get_current_humidity(current_humidity_data)) print(f"Current Temperature: {current_temperature}°C") print(f"Current Humidity: {current_humidity}%") # calculate time to reach target temperature and humidity temperature_difference_minutes = max(abs(target_temperature - current_temperature), 5) humidity_difference_minutes = max(abs(target_humidity - current_humidity), 5) print(f"Calculated Temperature Difference (minutes): {temperature_difference_minutes}") print(f"Calculated Humidity Difference (minutes): {humidity_difference_minutes}") # time difference conversion temperature_hours = int(temperature_difference_minutes // 60) temperature_minutes = int(temperature_difference_minutes % 60) humidity_hours = int(humidity_difference_minutes // 60) humidity_minutes = int(humidity_difference_minutes % 60) print(f"Temperature hours: {temperature_hours}, Temperature minutes: {temperature_minutes}") print(f"Humidity hours: {humidity_hours}, Humidity minutes: {humidity_minutes}") # configure and start aging chamber program program_steps = [ Program(temperature=target_temperature, humidity=0, hour=temperature_hours, minute=temperature_minutes), Program(temperature=target_temperature, humidity=target_humidity, hour=humidity_hours, minute=humidity_minutes), Program(temperature=target_temperature, humidity=target_humidity, hour=99, minute=0) ] for step in program_steps: print(step) if GWSChamberCtrl.SetProgram(program_steps): GWSChamberCtrl.RunProgram() total_wait_minutes = temperature_difference_minutes + humidity_difference_minutes print(f"Waiting for the chamber to reach the target conditions... Estimated time: {total_wait_minutes} minutes") time.sleep(total_wait_minutes * 60) print("Chamber has reached the target temperature and humidity. Continuing with the next steps...") except Exception as e: raise 这段代码python2可以跑么
06-12
def run_aging_chamber(): try: if all([target_high_temperature, target_low_temperature, target_time]): # get current temperature and humidity chamber = GWSChamberCtrl.GWSChamberCtrl(port=chamber_serial_port) current_temperature_data = chamber.get_temperature() current_temperature = float(chamber.get_current_temperature(current_temperature_data)) print(f"Current Temperature: {current_temperature}°C") # calculate time to reach target temperature and humidity temp_reach_duration_min = int(max(abs(target_high_temperature - current_temperature), 10)) print(f"Calculated Temperature Difference (minutes): {temp_reach_duration_min}") # time difference conversion temp_reach_hours = temp_reach_duration_min // 60 temp_reach_minutes = temp_reach_duration_min % 60 target_time_hours = target_time // 60 target_time_minutes = target_time % 60 print(f"Time to reach target temperature: {temp_reach_hours} hours and {temp_reach_minutes} minutes") print(f"Duration of target time setting: {target_time_hours} hours and {target_time_minutes} minutes") # configure and start aging chamber program program_steps = [ GWSChamberCtrl.Program(temperature=target_high_temperature, humidity=0, hour=temp_reach_hours,minute=temp_reach_minutes) ] for _ in range(target_cycles): program_steps.append(GWSChamberCtrl.Program(temperature=target_high_temperature, humidity=0, hour=target_time_hours, minute=target_time_minutes)) program_steps.append(GWSChamberCtrl.Program(temperature=target_low_temperature, humidity=0, hour=target_time_hours,minute=target_time_minutes)) for step in program_steps: print(step) if chamber.SetProgram(program_steps): chamber.RunProgram() chamber_stabilize_wait_min = temp_reach_duration_min print(f"Waiting for the chamber to reach the target conditions... Estimated time: {chamber_stabilize_wait_min} minutes") time.sleep(chamber_stabilize_wait_min * 60) print("Chamber has reached the target temperature and humidity. Continuing with the next steps...") except Exception as e: raise 帮我改成,湿度设置时间最高是半小时
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值