参考:
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; } }; }; @Override public 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); } @Override protected 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 block e.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 block e.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 block e.printStackTrace(); } } } } } } @Override public 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>
效果预览: