在Android中实现多线程同步
2008-10-15 16:09
555 查看
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();
// Test log system.
//testLog();
setContentView(R.layout.main);
}
// This is a example to use android.util.Log.
public void testLog() {
Log.i(MultiThread_ACTIVITY_TAG, "=============================");
Log.d(MultiThread_ACTIVITY_TAG, "this is a DEBUG of MyAndroid. ");
Log.i(MultiThread_ACTIVITY_TAG, "this is a INFO of MyAndroid. ");
Log.w(MultiThread_ACTIVITY_TAG, "this is a WARNING of MyAndroid. ");
}
public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
Log.d(MultiThread_ACTIVITY_TAG, "Producer #" + this.number + " put: " + i);
//System.out.println("Producer #" + this.number + " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {}
}
}
}
public class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}
public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
Log.d(MultiThread_ACTIVITY_TAG, "Consumer #" + this.number + " got: " + value);
//System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
}
2. 上面的代码中引入了android.util.Log包, 以拥有debug。
运行上面的工程后,在dbg窗口的Logcat区,显示打出的log。
下面将以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();
// Test log system.
//testLog();
setContentView(R.layout.main);
}
// This is a example to use android.util.Log.
public void testLog() {
Log.i(MultiThread_ACTIVITY_TAG, "=============================");
Log.d(MultiThread_ACTIVITY_TAG, "this is a DEBUG of MyAndroid. ");
Log.i(MultiThread_ACTIVITY_TAG, "this is a INFO of MyAndroid. ");
Log.w(MultiThread_ACTIVITY_TAG, "this is a WARNING of MyAndroid. ");
}
public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
Log.d(MultiThread_ACTIVITY_TAG, "Producer #" + this.number + " put: " + i);
//System.out.println("Producer #" + this.number + " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {}
}
}
}
public class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}
public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
Log.d(MultiThread_ACTIVITY_TAG, "Consumer #" + this.number + " got: " + value);
//System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
}
2. 上面的代码中引入了android.util.Log包, 以拥有debug。
运行上面的工程后,在dbg窗口的Logcat区,显示打出的log。
相关文章推荐
- 在Android中实现多线程同步
- 【笔记】android 多线程同步实现方法
- Android实现树状多层可展开收起ListView
- android 的对话框实现方式
- Android中ListView下拉刷新的实现方法实例分析
- Android蓝牙的基本介绍与实现
- Android 半圆矩形的实现
- Android实现ListView头部和尾部可伸展并回弹功能
- android 实现静默安装、卸载
- Android仿360悬浮小球自定义view实现示例
- Android 使用ProgressBar实现进度条
- Android沉浸式任务栏的实现
- Android如何实现图片轮播
- Android平板开发永久实现全屏的方法
- Android实现网络多线程断点续传下载
- 让Android自带的Gallery实现多点缩放,拖动和边界回弹效果,效果流畅
- Android中自定义MultipartEntity实现文件上传以及使用Volley库实现文件上传
- Android实现pppoe拨号上网(一)概述
- Android MediaRecorder实现暂停断点录音功能
- Android多线程异步处理:AsyncTask 的实现原理