在Android中实现多线程同步

技术博客 (140) 2023-09-16 16:08:01

 1. java中对多线程访问控制可使用关键字synchronized

下面将以
Producer Consumer模型介绍android中线程同步的使用。

步骤:
建立一android project,
修改main activity如下:

代码:

package com.test.thread;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MultiThread extends Activity {

   
    private static final String MultiThread_ACTIVITY_TAG = "MultiThread_TAG";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        CubbyHole c = new CubbyHole();
        Producer p1 = new Producer(c, 1);
        Consumer c1 = new Consumer(c, 1);
       
        p1.start();
        c1.start();
       

THE END

发表回复