两个线程执行生产 * 两线程执行消费 * 依次交替循环
2014-09-01 16:42
585 查看
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
* 两个线程执行生产
* 两线程执行消费
* 依次交替循环
* lock的方法(类的方法) 针对多线程访问此方法 只允许一个线程执行
* */
public class IntputOutputDome2 {
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
int x = 0;
while(true){
if(x==0){
Res2.set("敬先林", "男");
}
else{
Res2.set("张昊", "女");
}
x = (x+1)%2;
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
int x = 0;
while(true){
if(x==0){
Res2.set("敬先林", "男");
}
else{
Res2.set("张昊", "女");
}
x = (x+1)%2;
}
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
while(true)
Res2.out();
}
});
Thread t4 = new Thread(new Runnable() {
public void run() {
while(true)
Res2.out();
}
});
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Res2{
private static String name;
private static String sex;
private static boolean b = false;
private static Lock lock = new ReentrantLock();
static Condition c1 = lock.newCondition();
static Condition c2 = lock.newCondition();
public static void set(String name1,String sex1){
lock.lock();
try {
while(b){
c1.await();
}
name=name1;
sex=sex1;
System.out.println(Thread.currentThread().getName()+"......生产......"+name+"----"+sex);
b = true;
c2.signal();
} catch (Exception e) {
// TODO: handle exception
}
finally{
lock.unlock();
}
}
public static void out(){
lock.lock();
try {
while(!b){
c2.await();
}
System.out.println(Thread.currentThread().getName()+"......消费................."+name +"----"+sex);
b = false;
c1.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
lock.unlock();
}
}
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
* 两个线程执行生产
* 两线程执行消费
* 依次交替循环
* lock的方法(类的方法) 针对多线程访问此方法 只允许一个线程执行
* */
public class IntputOutputDome2 {
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
int x = 0;
while(true){
if(x==0){
Res2.set("敬先林", "男");
}
else{
Res2.set("张昊", "女");
}
x = (x+1)%2;
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
int x = 0;
while(true){
if(x==0){
Res2.set("敬先林", "男");
}
else{
Res2.set("张昊", "女");
}
x = (x+1)%2;
}
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
while(true)
Res2.out();
}
});
Thread t4 = new Thread(new Runnable() {
public void run() {
while(true)
Res2.out();
}
});
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Res2{
private static String name;
private static String sex;
private static boolean b = false;
private static Lock lock = new ReentrantLock();
static Condition c1 = lock.newCondition();
static Condition c2 = lock.newCondition();
public static void set(String name1,String sex1){
lock.lock();
try {
while(b){
c1.await();
}
name=name1;
sex=sex1;
System.out.println(Thread.currentThread().getName()+"......生产......"+name+"----"+sex);
b = true;
c2.signal();
} catch (Exception e) {
// TODO: handle exception
}
finally{
lock.unlock();
}
}
public static void out(){
lock.lock();
try {
while(!b){
c2.await();
}
System.out.println(Thread.currentThread().getName()+"......消费................."+name +"----"+sex);
b = false;
c1.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
lock.unlock();
}
}
}
相关文章推荐
- 嵌套的for循环执行顺序。可以让内外for循环交替执行。简单解析json数组。求js中两个时间差值。
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数
- 【多线程】两个线程 交替执行
- java 中的wait和notify以及synchronized的使用,实现两个线程交替执行
- 如何让A、B两个线程依次执行?
- 面试必问!Java 多线程中两个线程交替执行,一个输出偶数,一个输出奇数
- java两个线程交替执行
- 两个线程交替执行
- C#中添加三个线程同时启动执行某一方法,并依次调用某方法中的循环打印输。
- C#中添加三个线程同时启动执行某一方法,并依次调用某方法中的循环打印输。
- 两个线程按特定时间顺序交替执行
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数(方法一)
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数(方法二)
- 两个线程严格交替执行java实现
- 实现两个线程A,B,A打印5个奇数,B打印5个偶数,交替进行,如此循环50次
- 两个线程交替执行,一个打印1-52;一个打印A-Z;打印顺序为12A34B56C.....5152Z
- 简单的线程同步问题:两个线程交替执行N次【Synchronized、Lock、ArrayBlockingQueue】
- 两个线程交替执行
- Java案例:两个线程交替执行
- linux下,两个线程依次执行