* 迅雷笔试题:
* 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
由于线程执行的不确定性,要保证这样有序的输出,必须控制好多线程的同步。
线程同步有两种基本方法:synchronized
(2) wait,notify,notifyAll
/**
* 锁码:公共数据区
* 码值:码值为A,表示应该由A线程来执行,B,C线程等待
* 码值为B,C同理。
*/
class LockCode{
/**当前锁码码值,初始码值为A,表示最初由A线程运行*/
private char code='A';
/**单例模式*/
private LockCode(){
} public static LockCode newInstance(){
return new LockCode();
}
/**
* 循环设置锁码
* 每一次调用,锁码按照A-B-C-A-...-的顺序循环往复
*/
public void setCode(){
this.code=(char)(this.code+1);
if(this.code=='D')
this.code='A';
}
/**
* 得到锁码
*/
public char getCode(){
return this.code;
}
}
/**
* 完成打印工作的线程类
*/
class PrintRunnable implements Runnable{
/**需要打印的字符*/
private char character='?';
/**公共锁码*/
private LockCode lockCode=null; PrintRunnable(char c,LockCode l){
this.character=c;
this.lockCode=l;
}
/**
* 线程执行
*/
public void run() {
int loopCount=1;
while(loopCount<=10){
synchronized(lockCode){//线程同步操作锁码
try{
//如果当前运行的线程并不等于当前锁码的码值,则改线程等待
//比如当前运行线程是A,但是码值为B,则A线程等待。
while(lockCode.getCode()!=this.character)
lockCode.wait();
//码值匹配成功,打印字符
System.out.print(this.character);
//循环10次记数
loopCount++;
//设置码值,让下一个线程可以运行
lockCode.setCode();
//让其他所有等待线程激活
lockCode.notifyAll();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
} }
/**
* 测试
*/
public class ThreadLoopPrint { public static void main(String[] args) {
LockCode lockCode=LockCode.newInstance();//公共锁码
Thread ta=new Thread(new PrintRunnable('A',lockCode));
Thread tb=new Thread(new PrintRunnable('B',lockCode));
Thread tc=new Thread(new PrintRunnable('C',lockCode));
ta.start();
tb.start();
tc.start();
}
}
上面的模式叫:Guarded Suspension模式。
(参考:http://www.riabook.cn/doc/designpattern/GuardedSuspension.htm
先要考虑到,缓冲区会同时被两个以上的执行绪进行存取,即伺服器的请求处理执行绪与客户端执行绪,所以必须对缓冲区进行防护。
再来是当缓冲区中没有请求时,伺服器必须等待直到被通知有新的请求。
Guarded Suspension模式关注的是执行的流程架构,以Java来实现这个架构的话如下所示:
- RequestQueue.java
public class RequestQueue {
private java.util.LinkedList queue;
public RequestQueue() {
queue = new java.util.LinkedList();
} public synchronized Request getRequest() {
while(queue.size() <= 0) {
try {
wait();
}
catch(InterruptedException e) {}
}
return (Request) queue.removeFirst();
} public synchronized void putRequest(Request request) {
queue.addLast(request);
notifyAll();
}
}
一个例子是多人聊天伺服器,请求可能只是一个客户端送出的聊天讯息,聊天讯息会先存至缓冲区中,伺服器会不断的从缓冲区中取出 聊天讯息并发给客户端,如果缓冲区中没有新讯息,则伺服器就进入等待,直到有一个客户端发出聊天讯息并存入缓冲区中,此时伺服器再度被通知,然后再度取出 讯息并进行发送。
代码2:
/**
*此代码和上面的代码有一个很大的相同点,就是都利用公共数据区中的数据变化来决定线程工作还是阻塞等待。公共数据区利用了类静态变量。因此代码简洁。
*
*@author jiangtao
*@date 2010-2-27
*@version 1.0
*/
public class MyThread extends Thread{ public static String[] NAMES = new String[] { "A", "B", "C" }; public static int POS = 0; private static final long DURATION = 1000; private int count = 10; public MyThread (String name) {
this.setName(name);
} @Override
public void run() {
while (count > 0) {
if (this.getName().equals(NAMES[POS])) {
this.print();
this.count--;
}
try {
Thread.sleep(DURATION);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} private synchronized void print() { //不要sync也可以,因为我们的if限制。
System.out.print(this.getName());
POS = (POS >= NAMES.length - 1 ? 0 : ++POS);
} public static void main(String[] args) {
new MyThread ("A").start();
new MyThread ("B").start();
new MyThread ("C").start();
}
}
转自:http://hxraid.iteye.com/blog/607228
lpthread解法:
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
引申了一下:
有n个线程,ID为0...n-1,在屏幕上循环打印m次012..n-1
用 c/pthread 实现
//@Author : idup2x@gmail.com
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h> #define GROUP_COUNT 100
#define GROUP_SIZE 4 typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
int index;
} syn_obj_t; syn_obj_t syn_obj = {PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER, }; typedef struct {
int flag;
} elem_t; void *
thread_routine(void *arg); int
main(int argc, char** argv)
{
elem_t elems[GROUP_SIZE];
pthread_t pds[GROUP_SIZE];
int i; printf("syn_obj.index = %d\n", syn_obj.index); for (i = ; i < GROUP_SIZE; i++) {
elems[i].flag = i;
if ( (pthread_create(&pds[i], NULL, thread_routine, &elems[i])) != ) {
perror("pthread create");
exit(-);
}
} for (i = ; i < GROUP_SIZE; i++) {
pthread_join(pds[i], NULL);
} pthread_mutex_destroy(&syn_obj.mutex);
pthread_cond_destroy(&syn_obj.cond); printf("\nsyn_obj.index = %d\n", syn_obj.index); return ;
} void *
thread_routine(void *arg) {
elem_t *elem = (elem_t *)arg;
int i;
for (i = ; i < GROUP_COUNT; i++) {
pthread_mutex_lock(&syn_obj.mutex);
while ( (syn_obj.index % GROUP_SIZE) != elem->flag ) {
pthread_cond_wait(&syn_obj.cond, &syn_obj.mutex);
}
printf("%d", elem->flag);
if ( == (syn_obj.index+) % GROUP_SIZE ) {
printf("\t");
}
syn_obj.index++;
pthread_cond_broadcast(&syn_obj.cond);
// may be cause deadlock
// pthread_cond_signal(&syn_obj.cond);
pthread_mutex_unlock(&syn_obj.mutex);
// sleep(1);
}
return NULL;
}
root@iZ23onhpqvwZ: ./cond2
syn_obj.index = 0
0123 0123 0123 0123 0123 0123 0123 0123 0123 0123
syn_obj.index = 40