用java编写程序,利用线程同步编写一个栈操作程序,包括数据的进栈和出栈。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java编写程序,利用线程同步编写一个栈操作程序,包括数据的进栈和出栈。相关的知识,希望对你有一定的参考价值。
利用线程同步编写一个栈操作程序,包括数据的进栈和出栈。其中的数据和进栈与出栈的方法都是不同线程所共享的。为了演示,要求进栈10次,出栈10次,数据是0~500之间的随机整数,每次进栈、出栈后都随机休息0~500毫秒。
提示:1)编写一个栈类(Stack)。该类包括一个数组data及数组下标index两个属性,其中data用来存放栈中的数据,index用来指示数组中元素的位置。另外定义两个同步方法popup()和push(),用来向栈中读出或写入数据。2)编写一个线程类,里面包含向栈中写入数据的线程和从栈中读出数据的线程;3)编写一个主类,实现向栈中写入和读出数据。
Stack.java
import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Stack
private int[] data;
private int index;
private Lock lock;
private Condition moreSpace;
private Condition moreEelment;
public Stack(int size)
this.data = new int[size];
this.index = 0;
this.lock = new ReentrantLock();
this.moreSpace = lock.newCondition();
this.moreEelment = lock.newCondition();
public void push(int value)
lock.lock();
try
while(index == data.length)
moreSpace.await();
data[index++] = value;
moreEelment.signalAll();
catch (InterruptedException e)
e.printStackTrace();
finally
lock.unlock();
public int popup()
lock.lock();
int value = 0;
try
while(index == 0)
moreEelment.await();
value = data[--index];
moreSpace.signalAll();
catch (InterruptedException e)
e.printStackTrace();
finally
lock.unlock();
return value;
写入线程 WriteStack.java
public class WriteStack implements Runnable
private Stack stack;
public WriteStack(Stack stack)
this.stack = stack;
@Override
public void run()
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i < 10; i++)
int value = r.nextInt(500);
stack.push(value);
System.out.printf("Write: push %d in stack\\n",value);
try
Thread.sleep(r.nextInt(500));
catch (InterruptedException e)
e.printStackTrace();
读取线程 ReadStack.java
import java.util.Random;public class ReadStack implements Runnable
private Stack stack;
public ReadStack(Stack stack)
this.stack = stack;
@Override
public void run()
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i < 10; i++)
int value = stack.popup();
System.out.printf("Read: popup an element %d\\n",value);
try
Thread.sleep(r.nextInt(500));
catch (InterruptedException e)
e.printStackTrace();
主测试线程 StackExample.java
public class StackExamplepublic static void main(String[] args) throws InterruptedException
Stack stack = new Stack(5);
WriteStack writeStack = new WriteStack(stack);
ReadStack readStack = new ReadStack(stack);
Thread writeThread = new Thread(writeStack);
Thread readThread = new Thread(readStack);
writeThread.start();
readThread.start();
参考技术A
Java多线程
题目一:
编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。
代码:
1、Test.java
1 package cn.edu.ccut; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 Thread T = new Thread(new ThreadObject());//利用Thread类创建线程对象; 7 T.start(); 8 } 9 10 }
2、ThreadObject
1 package cn.edu.ccut; 2 3 import java.util.Date; 4 5 public class ThreadObject implements Runnable { 6 7 @Override 8 public void run() { 9 Date date; 10 while(true){ 11 date = new Date();//获取时间; 12 System.out.println(date); 13 try { 14 Thread.sleep(1000); //使线程每隔一秒休眠; 15 } catch (InterruptedException e) { 16 e.printStackTrace(); 17 } 18 } 19 } 20 }
运行结果:
以上是关于用java编写程序,利用线程同步编写一个栈操作程序,包括数据的进栈和出栈。的主要内容,如果未能解决你的问题,请参考以下文章