wait和sleep的區別 以及 執行個體示範

來源:互聯網
上載者:User

參考:

Java多線程中Sleep與Wait的區別  http://uule.iteye.com/blog/1106710
關於多線程的wait與sleep的區別  http://www.iteye.com/topic/414054

有關於wait和sleep的區別,請先拜讀以上文章。上面已經講的很清楚了。

這裡我主要根據自己的理解,在執行個體中運用wait和sleep,以加深對兩者之間一個關鍵區別的理解。(wait會釋放對象鎖,而sleep則不會釋放對象鎖。)

執行個體主要流程:

Wait:

Sync_Wait 線程運行到i=20的時候,調用wait等待。此時mObject對象鎖已經釋放,Sync_Wait_Normal開始運行。

當Sync_Wait_Normal運行到i=50時,調用mObject.notify();喚醒了Sync_Wait。但由於此時,Sync_Wait無法得到mObject對象鎖而無法立即運行。

等到Sync_Wait_Normal運行到i=80時,調用wait,Sync_Wait_Normal開始進行線程等待,並釋放mObject對象鎖。

這時,Sync_Wait 進行運行(從i=21開始)。當Sync_Wait 運行到i=50時,調用mObject.notify();喚醒了Sync_Wait_Normal。但由於此時,Sync_Wait_Normal無法得到mObject對象鎖而無法立即運行。所以,一直到Sync_Wait運行到i=99,也就是Sync_Wait運行結束後,釋放mObject對象鎖。這個時候,Sync_Wait_Normal才繼續運行到最後。

Sleep:

Sync_Sleep線程運行到i=20時,調用Thread.sleep,使得Sync_Sleep休眠2秒鐘。但由於此時sleep並沒有釋放mObject對象鎖。因此,Sync_Sleep_Normal此時也無法獲得mObject對象鎖而運行。等到Sync_Sleep因為sleep的時間過來,自己醒來,並運行結束後,Sync_Sleep_Normal才從頭開始運行,一直到結束。

所以,整個過程就會使這樣的:Sync_Sleep運行到i=20,在此等待2秒(會出現停頓2s現象)。接著繼續運行到i=99,Sync_Sleep運行完成並結束,釋放mObject對象鎖。接著,Sync_Sleep_Normal獲得mObject對象鎖,從頭開始運行,一直到結束。

執行個體源碼下載:http://download.csdn.net/detail/yang_hui1986527/4429503

MainActivity.java

package com.snowdream.demo;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {private static final int MSG_CLEAR = 0;private static final int MSG_UPDATE = 1;private final Object mObject = new Object();private final String tag = "MainActivity";ExecutorService pool = null; private static TextView mTextView = null;private static Handler mHandler = new Handler(){public void handleMessage(Message msg){switch (msg.what) {case MSG_CLEAR:mTextView.setText("");break;case MSG_UPDATE:String str = (String)msg.obj;if (!TextUtils.isEmpty(str)) {mTextView.append(str);mTextView.append("\n");}break;default:break;}};};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();}private void initView() {mTextView = (TextView)findViewById(R.id.textView1);}private void initData() {pool = Executors.newFixedThreadPool(2); }@Overrideprotected void onDestroy() {super.onDestroy();pool.shutdown();}public void OnButton1Click(View view) {int id = view.getId();switch (id) {case R.id.button1:mHandler.sendMessage(mHandler.obtainMessage(MSG_CLEAR));Sync_Wait sync_Wait = new Sync_Wait();Sync_Wait_Normal sync_Wait_Normal = new Sync_Wait_Normal();pool.execute(sync_Wait);pool.execute(sync_Wait_Normal);break;case R.id.button2:mHandler.sendMessage(mHandler.obtainMessage(MSG_CLEAR));Sync_Sleep sync_Sleep  = new Sync_Sleep();Sync_Sleep_Normal sync_Sleep_Normal = new Sync_Sleep_Normal();pool.execute(sync_Sleep);pool.execute(sync_Sleep_Normal);break;default:break;}}public class Sync_Wait implements Runnable {public void run() {synchronized(mObject){for (int i = 0; i < 100; i++) {Log.i(tag, "Sync_Wait: "+ i);mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE, "Sync_Wait: "+ i));if (20 == i) {try {mObject.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (50 == i) {mObject.notify();}}}}} public class Sync_Sleep implements Runnable {public void run() {synchronized(mObject){for (int i = 0; i < 100; i++) {Log.i(tag, "Sync_Sleep_Normal: "+ i);mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE,  "Sync1_Sleep: "+ i));if (20 == i) {try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}} public class Sync_Sleep_Normal implements Runnable {public void run() {synchronized(mObject){for (int i = 0; i < 100; i++) {Log.i(tag, "Sync_Sleep_Normal: "+ i);mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE, "Sync_Sleep_Normal: "+ i));}}}} public class Sync_Wait_Normal implements Runnable {public void run() {synchronized(mObject){for (int i = 0; i < 100; i++) {Log.i(tag, "Sync_Normal: "+ i);mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE, "Sync_Wait_Normal: "+ i));if (50 == i) {mObject.notify();}if (80 == i) {try {mObject.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}} @Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true" >        <Button            android:id="@+id/button1"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:onClick="OnButton1Click"            android:text="Wait" />        <Button            android:id="@+id/button2"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:onClick="OnButton1Click"            android:text="Sleep" />           </LinearLayout>    <ScrollView        android:id="@+id/scrollView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/linearLayout1"        android:layout_toLeftOf="@+id/textView1" >        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="@dimen/padding_small"            tools:context=".MainActivity" />    </ScrollView></RelativeLayout>

效果預覽:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.