linux多线程共享buffer,1005实现一个线程从共享的缓冲区中读数据,另一个线程向共享的缓冲区中写数据...

/*

编写一个程序,实现一个线程从共享的缓冲区中读数据,另一个线程向共享的缓冲区中写数据

对共享的缓冲区的访问控制是通过使用一个互斥锁来实现的。

*/

#include

#include

#include

#include

#define FALSE 0

#define TRUE 1

void readfun();

void writefun();

char buffer[256];

int buffer_has_item=0;

int retflag=FALSE;

pthread_mutex_t mutex;

int main(int argc, char argv[])

{

pthread_t reader;

pthread_mutex_init(&mutex,NULL);

pthread_create(&reader,NULL,(void)&readfun,NULL);

writefun();

}

void readfun()

{

while(1)

{

if(retflag)

return;

pthread_mutex_lock(&mutex);

if(buffer_has_item==1)

{

printf("%s",buffer);

buffer_has_item=0;

}

pthread_mutex_unlock(&mutex);

}

}

void writefun()

{

int i=0;

while(1)

{

if(i==10)

{

retflag=TRUE;

return;

}

pthread_mutex_lock(&mutex);

if(buffer_has_item==0)

{

sprintf(buffer,"This is %d\n",i++) ;

buffer_has_item=1;

}

pthread_mutex_unlock(&mutex);

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来解答这个问题。首先,我们需要定义一个4位循环缓冲区(也叫循环队列),用来存储生成的随机数。我们可以使用数组来实现这个缓冲区。 接下来,我们需要实现两个线程一个线程向循环缓冲区中写数据,另一个线程从循环缓冲区读取数据并打印出来。这里我们可以使用Java线程和锁来实现。 下面是两种方法的具体实现: 方法一:使用synchronized关键字实现线程同步 ```java import java.util.Random; public class Test { private static final int BUFFER_SIZE = 4; private static int[] buffer = new int[BUFFER_SIZE]; private static int count = 0; private static int writeIndex = 0; private static int readIndex = 0; public static void main(String[] args) { Thread writerThread = new Thread(() -> { Random random = new Random(); while (true) { synchronized (Test.class) { if (count == BUFFER_SIZE) { try { Test.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int num = random.nextInt(10) + 1; buffer[writeIndex] = num; writeIndex = (writeIndex + 1) % BUFFER_SIZE; count++; Test.class.notify(); } } }); Thread readerThread = new Thread(() -> { while (true) { synchronized (Test.class) { if (count == 0) { try { Test.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int num = buffer[readIndex]; readIndex = (readIndex + 1) % BUFFER_SIZE; count--; System.out.println(num); Test.class.notify(); } } }); writerThread.start(); readerThread.start(); } } ``` 在这个实现,我们使用了synchronized关键字来保证多个线程共享变量进行操作的线程安全性。当缓冲区已满时,写线程会进入等待状态,直到读线程取走了一个数据,释放了一个空位。同样,当缓冲区为空时,读线程会进入等待状态,直到写线程写入了一个数据,释放了一个位置。 方法二:使用ReentrantLock类实现线程同步 ```java import java.util.Random; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class Test { private static final int BUFFER_SIZE = 4; private static int[] buffer = new int[BUFFER_SIZE]; private static int count = 0; private static int writeIndex = 0; private static int readIndex = 0; private static ReentrantLock lock = new ReentrantLock(); private static Condition notFull = lock.newCondition(); private static Condition notEmpty = lock.newCondition(); public static void main(String[] args) { Thread writerThread = new Thread(() -> { Random random = new Random(); while (true) { lock.lock(); try { while (count == BUFFER_SIZE) { notFull.await(); } int num = random.nextInt(10) + 1; buffer[writeIndex] = num; writeIndex = (writeIndex + 1) % BUFFER_SIZE; count++; notEmpty.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }); Thread readerThread = new Thread(() -> { while (true) { lock.lock(); try { while (count == 0) { notEmpty.await(); } int num = buffer[readIndex]; readIndex = (readIndex + 1) % BUFFER_SIZE; count--; System.out.println(num); notFull.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } }); writerThread.start(); readerThread.start(); } } ``` 在这个实现,我们使用了ReentrantLock和Condition类来实现线程同步。ReentrantLock类提供了锁机制,Condition类提供了等待和通知机制。当缓冲区已满时,写线程会进入等待状态,直到读线程取走了一个数据,释放了一个空位。同样,当缓冲区为空时,读线程会进入等待状态,直到写线程写入了一个数据,释放了一个位置。 以上就是用两种方法编写一个Java程序的详细介绍。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值