本文採用lock以及unlock做測試
採用
private Object lock = new Object();
lock.wait();
lock.notify();
------
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jing.example.nknu_.multiprocess_p25">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.jing.example.nknu_.multiprocess_p25.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.multiprocess_p25.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生產"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="218dp"
android:onClick="StartProduce" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消費"
android:id="@+id/button2"
android:onClick="StartConsume"
android:layout_alignTop="@+id/button"
android:layout_alignRight="@+id/textView5"
android:layout_alignEnd="@+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="物品庫存量"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生產狀態"
android:id="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_alignTop="@+id/textView"
android:layout_alignBottom="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消費狀態"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView6"
android:layout_below="@+id/textView5"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5" />
</RelativeLayout>
MainActivity.java
package com.jing.example.nknu_.multiprocess_p25;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity {
final ConsumerProducer cp = new ConsumerProducer();
public class ConsumerProducer{
private LinkedList<Integer> list = new LinkedList<Integer>();
private Object lock = new Object();
public void produce() throws InterruptedException{
synchronized(lock){
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView txv;
txv = (TextView) findViewById(R.id.textView4);
txv.setText("Lock");
}
});
lock.notify();//通知consume可以消費
lock.wait();//produce 鎖住
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView txv;
txv = (TextView) findViewById(R.id.textView4);
txv.setText("unLock");
}
});
}
}
public void consume() throws InterruptedException{
synchronized (lock) {
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView txv;
txv = (TextView) findViewById(R.id.textView6);
txv.setText("Lock");
}
});
lock.notify();//通知 produce 可以生產
lock.wait();//consume鎖住
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView txv;
txv = (TextView) findViewById(R.id.textView6);
txv.setText("unLock");
}
});
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txv;
txv = (TextView)findViewById(R.id.textView2);
txv.setText("0");
}
public void StartProduce(View v) throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
try {
cp.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public void StartConsume(View v) throws InterruptedException {
new Thread(new Runnable(){
@Override
public void run() {
try {
cp.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}