多线程join(),wait(),nitify()运用
2016-08-11 17:59
357 查看
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
}
}
}
相关文章推荐
- Java第七课 Java的多线程程序进程和线程的概念,实现多线程的两种方式,线程同步的原理,线程的死锁,运用wait和notify来实现producer - consumer关系,线程终止的两种情况。
- 多线程编程里如何运用CreateMutex,WaitForSingleObject,ReleaseMutex来实现线程同步
- Java多线程wait,notify以及同步锁的运用实例
- java线程的中断(wait 和notifyall的运用)
- 线程通信,wait()与notify()的运用
- java线程的wait方法和notify方法的运用
- 【郭林专刊】多线程知识总结线程通信,wait()与notify()的运用
- 运用java中的wait和notifyAll方法模拟玩家等待游戏进入场景
- java线程的wait方法和notify方法的运用
- Java【多线程知识总结(10)】线程通信之wait()与notify()的运用--模拟指挥官指挥2个连队交替轰炸战区
- 初阶并发编程之——运用wait()与notify()实现线程之间的协作
- Java【多线程知识总结(10)】线程通信之wait()与notify()的运用--模拟指挥官指挥2个连队交替轰炸战区
- wait和notify工作运用记录
- Java【多线程知识总结(8)】线程通信,wait()与notify()的运用
- Java【多线程知识总结(8)】线程通信,wait()与notify()的运用
- java线程的wait方法和notify方法的运用(包含生产者消费者代码【已练习】)
- Java【多线程知识总结(10)】线程通信之wait()与notify()的运用--模拟指挥官指挥2个连队交替轰炸战区<另外的写法>
- 如何知道Hibernate已经成功运用了二级缓存EhCache
- 第七章 灵活运用寻址方式(si/di和内嵌套循环)——所有的习题练习
- 分析性能瓶颈的视图:v$session_wait