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;


/**

*夫妻轮流搬砖,这是最简单的只有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