java wait和notify的使用

多线程同步,例如想要一个线程结束后再执行另外一个线程,可以在主线程中使用wait等待,子线程中使用notify唤醒主线程

下列例子:目的想执行完Beep循环后再执行Beef循环

package com.example.g;

import java.lang.reflect.Method;

import com.pax.api.BaseSystemManager;

import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Button;
import android.os.Message;

public class MainActivity extends Activity implements OnClickListener{
    private TextView text = null;
    private TextView tv = null;
    private Button startBt = null;
    private final static int OPEN_SUCCESS = 0;
    private final static int OPEN_FAIL = 1;
    private final static int ClOSE_SUCCESS = 2;
    private final static int CLOSE_FAIL = 3;
    private final static int START_TEST = 4;
    private final static int NOTIFY = 5;
    private boolean flag = false;

    private BaseSystemManager baseSystemManager = null;
    private static Boolean waitNotifyFlag = Boolean.valueOf(true);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);
        tv = (TextView)findViewById(R.id.tv);
        startBt = (Button)findViewById(R.id.start);
        startBt.setOnClickListener(this);
        text.setText(getResources().getString(R.string.start));
        try{
            baseSystemManager = BaseSystemManager.getInstance();
        }catch(Exception e){

        }
    }

    public void onClick(View v) {
        text.setText(getResources().getString(R.string.start_test));
        Thread testThread = new GThread();
        testThread.start();
        Message msg = new Message();

        // 等待通信线程完成测试
        try {
            synchronized (waitNotifyFlag) {
                while(!flag){
                    waitNotifyFlag.wait();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        SystemClock.sleep(2000);
        msg.what = START_TEST;
        msg.obj = (Object)getResources().getString(R.string.wait);
        handler.sendMessage(msg);
        int i;
        for(i = 0; i < 10; i++){
            try{
                baseSystemManager.beef((byte)0, 1000);
            }catch(Exception e){}
        }
    }

    class GThread extends Thread {
        public void run() {
            Message msg = new Message();
            int i;
            for(i = 0; i < 1000; i++){
                try{
                    baseSystemManager.beep();
                }catch(Exception e){}
            }

            SystemClock.sleep(3000);
            // 测试完成,唤醒主线程
            synchronized (waitNotifyFlag) {
                flag = true;
                waitNotifyFlag.notifyAll();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            switch(msg.what){
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                    text.setText(msg.obj.toString());
                    break;
                case 5:
                    tv.setText(msg.obj.toString());
                    break;

            }
        }
    };

    }
}

你可能感兴趣的:(java,android,wait,java)