import javax.swing.event.CellEditorListener;
public class test {
public static void main(String[] args) {
test1 test1=new test1();
Input input=new Input(test1);
Output output=new Output(test1);
new Thread(input).start();
new Thread(output).start();
}
}
class Input implements Runnable{
private test1 st;
private int sum;//定义一个变量
public Input(test1 st) {//通过构造方法接受一个TEST对象
// TODO Auto-generated constructor stub
this.st=st;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
st.put(sum++);//将sun存入数组,每次存入后sum自增
}
}
}
class Output implements Runnable{
private test1 st;
public Output(test1 st) {
// TODO Auto-generated constructor stub
this.st=st;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
st.get();//循环取出元素
}
}
}
class test1{
//数据存储数组
private int[] cslls=new int[10];
//inpos表示存入时数组下标,outpos表示取出时数组的下标
private int inpos,outpos;
//存放取出数据的数量
private int coun;
//定义一个put()方法向数组中存入数据
public synchronized void put(int num){
try {
//当放入数据等于数组长度的时候,线程等待
while (coun==cslls.length) {
this.wait();
}
cslls[inpos]=num;
System.out.println("在cslls[inpos]中放入数据-----------------"+cslls[inpos]);
inpos++;//存完数据后让下标加1;
if(inpos==cslls.length)
inpos=0;//当inpos为数组长度时候,将其置位0;
coun++;
this.notify();
} catch (Exception e) {
// TODO: handle exception
}
}
//定义一个get()方法从数组中取出数据
public void get(){
try {
//当放入数据等于数组长度的时候,线程等待
while (coun==cslls.length) {
this.wait();
}
int data=cslls[outpos];
System.out.println("从cslls[inpos]中取出数据"+data);
outpos++;
if (outpos==cslls.length)
outpos=0;
this.notify();
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class test {
public static void main(String[] args) {
test1 test1=new test1();
Input input=new Input(test1);
Output output=new Output(test1);
new Thread(input).start();
new Thread(output).start();
}
}
class Input implements Runnable{
private test1 st;
private int sum;//定义一个变量
public Input(test1 st) {//通过构造方法接受一个TEST对象
// TODO Auto-generated constructor stub
this.st=st;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
st.put(sum++);//将sun存入数组,每次存入后sum自增
}
}
}
class Output implements Runnable{
private test1 st;
public Output(test1 st) {
// TODO Auto-generated constructor stub
this.st=st;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
st.get();//循环取出元素
}
}
}
class test1{
//数据存储数组
private int[] cslls=new int[10];
//inpos表示存入时数组下标,outpos表示取出时数组的下标
private int inpos,outpos;
//存放取出数据的数量
private int coun;
//定义一个put()方法向数组中存入数据
public synchronized void put(int num){
try {
//当放入数据等于数组长度的时候,线程等待
while (coun==cslls.length) {
this.wait();
}
cslls[inpos]=num;
System.out.println("在cslls[inpos]中放入数据-----------------"+cslls[inpos]);
inpos++;//存完数据后让下标加1;
if(inpos==cslls.length)
inpos=0;//当inpos为数组长度时候,将其置位0;
coun++;
this.notify();
} catch (Exception e) {
// TODO: handle exception
}
}
//定义一个get()方法从数组中取出数据
public void get(){
try {
//当放入数据等于数组长度的时候,线程等待
while (coun==cslls.length) {
this.wait();
}
int data=cslls[outpos];
System.out.println("从cslls[inpos]中取出数据"+data);
outpos++;
if (outpos==cslls.length)
outpos=0;
this.notify();
} catch (Exception e) {
// TODO: handle exception
}
}
}