Java多线程-对歌式互动-Lock-Condition
来源:互联网 发布:现货白银软件 编辑:程序博客网 时间:2024/05/30 05:14
package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
private static final int goldenCount = 4;//100 golden bricks
private static final int familyGroup = 1;
private static final String inputFile = "testInput.txt";
public static void generateTestFile() throws IOException{
PrintWriter pw = new PrintWriter(new FileWriter(new File(inputFile)),true);
Random random = new Random();
for(int i=0;i<goldenCount; i++){
pw.write(Math.abs(random.nextInt()) % goldenCount + ",");
}
pw.flush();
pw.close();
}
public static void main(String[] args) {
try{
generateTestFile();
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String str = reader.readLine();
reader.close();
String[] strs = str.split(",");
int index = 0;
int countForEachGroup = goldenCount/familyGroup;
for(int i=0; i < familyGroup; i++ ){
int records[] = new int[countForEachGroup];
for(int j=0; j<countForEachGroup; j++){
records[j] = Integer.parseInt(strs[index]);
index++;
}
Family family = new Family(records,i);
family.startFuck();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
class Family{
private static volatile int finishCount = 0;
private Lock lock = new ReentrantLock();
private Condition oddLock = lock.newCondition();
private Condition evenLock = lock.newCondition();
private int records[];//all the family need to load
private PrintWriter writer;
private volatile int oddIndex = 0;
private volatile int evenIndex = 0;
private OddPrintThread oddPrintThread;
private EvenPrintThread evenPrintThread;
private volatile boolean first = true;
private int[] result = new int[4];//20
private int index = 0;
public Family(int[] records,int id) throws Exception{
this.records = records;
this.writer = new PrintWriter(new FileWriter(new File("output"+id+".txt")),true);
}
public void startFuck(){
oddPrintThread = new OddPrintThread();
evenPrintThread = new EvenPrintThread();
oddPrintThread.start();
evenPrintThread.start();
}
private class OddPrintThread extends Thread{
public void run(){
while(true){
System.out.println("enter boy thread,while block ================= ");
try{
lock.lock();
if(first){
first = false;
System.out.println("this is boy thread:"+this.getName()+" who gonna to wait ");
evenLock.await();
}
for(int i=0; i<2;){//print 10
if(oddIndex >= records.length && evenIndex >= records.length ){
writer.flush();
writer.close();
return;
}
if(oddIndex >= records.length ){
break;
}
if(records[oddIndex]%2 == 1){//if not?try next
i++;
writer.println(records[oddIndex] + " ");
result[index++] = records[oddIndex];
writer.flush();
addCount(this.getName());
}
System.out.println("boy thread,this is thread:"+ this.getName() +" &i="+i+" &oddIndex="+oddIndex);
oddIndex++;
if(oddIndex >= records.length && evenIndex >= records.length ){
System.out.println("this group finish:"+this.getName()+" &man" );
}
}
oddLock.signal();
evenLock.await();
}catch(Exception ex){
ex.printStackTrace();
}finally{
oddLock.signal();
lock.unlock();
}
}
}
}
private class EvenPrintThread extends Thread{
public void run(){
while(true){
System.out.println("enter girl thread while ================");
try{
while(first){
System.out.println("enter girl thread while first***");
Thread.sleep(1);
}
lock.lock();
for(int i=0; i<2;){
if(oddIndex >= records.length && evenIndex >= records.length ){
writer.flush();
return;
}
if(evenIndex >= records.length ){//girl finish
break;
}
if(records[evenIndex] % 2 == 0){
i++;
writer.print(records[evenIndex] + " ");
result[index++] = records[evenIndex];
writer.flush();
addCount(this.getName());
}
System.out.println("girl thread,this is thread:"+ this.getName()+" &i="+i+" &evenIndex="+evenIndex);
evenIndex++;
if(oddIndex >= records.length && evenIndex >= records.length ){
System.out.println("this group finish:"+this.getName() + " &girl");
}
}
evenLock.signal();
oddLock.await();
}catch(Exception ex){
ex.printStackTrace();
}
finally{
evenLock.signal();
lock.unlock();
}
}//end of while
}//end of run
}//end of class
private synchronized static void addCount(String threadName){
finishCount++;
if(finishCount%2 == 0){
//System.out.println(threadName+" already finish:"+finishCount);
if(finishCount== 12){
System.out.println("Done!");
}
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*夫妻轮流搬砖,这是最简单的只有1对夫妻搬的情况。
*/
public class Test {private static final int goldenCount = 4;//100 golden bricks
private static final int familyGroup = 1;
private static final String inputFile = "testInput.txt";
public static void generateTestFile() throws IOException{
PrintWriter pw = new PrintWriter(new FileWriter(new File(inputFile)),true);
Random random = new Random();
for(int i=0;i<goldenCount; i++){
pw.write(Math.abs(random.nextInt()) % goldenCount + ",");
}
pw.flush();
pw.close();
}
public static void main(String[] args) {
try{
generateTestFile();
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String str = reader.readLine();
reader.close();
String[] strs = str.split(",");
int index = 0;
int countForEachGroup = goldenCount/familyGroup;
for(int i=0; i < familyGroup; i++ ){
int records[] = new int[countForEachGroup];
for(int j=0; j<countForEachGroup; j++){
records[j] = Integer.parseInt(strs[index]);
index++;
}
Family family = new Family(records,i);
family.startFuck();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
class Family{
private static volatile int finishCount = 0;
private Lock lock = new ReentrantLock();
private Condition oddLock = lock.newCondition();
private Condition evenLock = lock.newCondition();
private int records[];//all the family need to load
private PrintWriter writer;
private volatile int oddIndex = 0;
private volatile int evenIndex = 0;
private OddPrintThread oddPrintThread;
private EvenPrintThread evenPrintThread;
private volatile boolean first = true;
private int[] result = new int[4];//20
private int index = 0;
public Family(int[] records,int id) throws Exception{
this.records = records;
this.writer = new PrintWriter(new FileWriter(new File("output"+id+".txt")),true);
}
public void startFuck(){
oddPrintThread = new OddPrintThread();
evenPrintThread = new EvenPrintThread();
oddPrintThread.start();
evenPrintThread.start();
}
private class OddPrintThread extends Thread{
public void run(){
while(true){
System.out.println("enter boy thread,while block ================= ");
try{
lock.lock();
if(first){
first = false;
System.out.println("this is boy thread:"+this.getName()+" who gonna to wait ");
evenLock.await();
}
for(int i=0; i<2;){//print 10
if(oddIndex >= records.length && evenIndex >= records.length ){
writer.flush();
writer.close();
return;
}
if(oddIndex >= records.length ){
break;
}
if(records[oddIndex]%2 == 1){//if not?try next
i++;
writer.println(records[oddIndex] + " ");
result[index++] = records[oddIndex];
writer.flush();
addCount(this.getName());
}
System.out.println("boy thread,this is thread:"+ this.getName() +" &i="+i+" &oddIndex="+oddIndex);
oddIndex++;
if(oddIndex >= records.length && evenIndex >= records.length ){
System.out.println("this group finish:"+this.getName()+" &man" );
}
}
oddLock.signal();
evenLock.await();
}catch(Exception ex){
ex.printStackTrace();
}finally{
oddLock.signal();
lock.unlock();
}
}
}
}
private class EvenPrintThread extends Thread{
public void run(){
while(true){
System.out.println("enter girl thread while ================");
try{
while(first){
System.out.println("enter girl thread while first***");
Thread.sleep(1);
}
lock.lock();
for(int i=0; i<2;){
if(oddIndex >= records.length && evenIndex >= records.length ){
writer.flush();
return;
}
if(evenIndex >= records.length ){//girl finish
break;
}
if(records[evenIndex] % 2 == 0){
i++;
writer.print(records[evenIndex] + " ");
result[index++] = records[evenIndex];
writer.flush();
addCount(this.getName());
}
System.out.println("girl thread,this is thread:"+ this.getName()+" &i="+i+" &evenIndex="+evenIndex);
evenIndex++;
if(oddIndex >= records.length && evenIndex >= records.length ){
System.out.println("this group finish:"+this.getName() + " &girl");
}
}
evenLock.signal();
oddLock.await();
}catch(Exception ex){
ex.printStackTrace();
}
finally{
evenLock.signal();
lock.unlock();
}
}//end of while
}//end of run
}//end of class
private synchronized static void addCount(String threadName){
finishCount++;
if(finishCount%2 == 0){
//System.out.println(threadName+" already finish:"+finishCount);
if(finishCount== 12){
System.out.println("Done!");
}
}
}
}
0 0
- Java多线程-对歌式互动-Lock-Condition
- Java多线程-对歌式互动-5对夫妻
- Java多线程4- Lock、Condition
- Java多线程4- Lock、Condition
- java多线程Lock Condition生产消费者
- java多线程-07-Lock和Condition
- java多线程通信之lock和Condition
- Java多线程(五) ReentrantLock、Lock和Condition的用法
- JAVA多线程-Lock的使用(一)-ReentrantLock与Condition
- Java多线程编程4--Lock的使用--重入锁(ReentrantLock)、Condition
- Java多线程 之 lock与condition的使用(十四)
- JAVA多线程—Lock&Condition实现线程同步通信
- java多线程-Lock与Condition接口的使用详解
- JAVA多线程系列--Lock锁-ReentrantLock之Condition应用
- Java Lock Condition
- java lock condition
- java并发---lock和condition
- 多线程中使用Lock和Condition机制
- redis sentinel(哨兵)
- MySql 数据库的创建和查看
- Mysql 常见命令总结
- lua之 unpack
- 【通用】 使用 try catch 处理异常
- Java多线程-对歌式互动-Lock-Condition
- MySQL checksum了解
- js call和apply方法的了解
- js arguments和caller,callee
- mysql 常见函数总结
- Nginx-------Nginx的安装和多域名配置
- redis 安装
- Maven学习:常用mvn命令
- 每天一个Linux命令(31):du