JAVA多线程-根据lock代码重写

来源:互联网 发布:淘宝汽车座垫套 编辑:程序博客网 时间:2024/06/10 20:26
package com.thread2;
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;

public class DrawTest
{
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 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;

int records[] = new int[countForEachGroup];
for(int j=0; j<countForEachGroup; j++){
records[j] = Integer.parseInt(strs[index]);
System.out.println("records[j]="+ records[j]);
index++;
}

GoldenBrick acct = new GoldenBrick(records , 0);
new BoyThread("peter" , acct).start();
new GirlThread("mary" , acct).start();
}catch(Exception ex){
ex.printStackTrace();
}
}
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();
}

}

===================类2:

package com.thread2;
public class BoyThread extends Thread
{
private GoldenBrick account;
public BoyThread(String threadName , GoldenBrick account)
{
super(threadName);
this.account = account;
}
public void run()
{
account.boyMoveBrick();
}
}


============类3:

package com.thread2;
public class GirlThread extends Thread
{
private GoldenBrick account;
public GirlThread(String threadName , GoldenBrick account)
{
super(threadName);
this.account = account;
}
public void run()
{
account.girlMoveBrick();
}
}


================类4:

package com.thread2;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
public class GoldenBrick {

private static volatile int finishCount = 0;

private boolean ladyWaitFlag = false;

private PrintWriter writer;

private int records[];//all the family need to load

private int[] result = new int[4];//total bricks this couple should move

private volatile int oddIndex = 0;//the number for boy already checked

private volatile int evenIndex = 0;//the number for girl already checked

private int index = 0;

public GoldenBrick(int[] records,int id) throws Exception{
this.records = records;
this.writer = new PrintWriter(new FileWriter(new File( "output"+id+".txt" )),true);
}

public synchronized void boyMoveBrick(){
while(true){
System.out.println("enter boyMoveBrick while block ================= ");
try{
if(!ladyWaitFlag){
System.out.println("this is boyMoveBrick and gonna to wait ");
wait();
}
for(int i=0; i<2;){//move 2
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();
}

oddIndex++;
System.out.println("boyMoveBrick &i="+i+" &oddIndex="+oddIndex);
if(oddIndex >= records.length && evenIndex >= records.length ){
System.out.println("this group finish at boyMoveBrick" );
}
}
ladyWaitFlag = false;
notify();
}catch(Exception ex){
ex.printStackTrace();
}
}//end of while
}//end of boyMoveBrick


public synchronized void girlMoveBrick(){
while (true) {
System.out.println("enter girlMoveBrick while ================");
try {
if (ladyWaitFlag) {
System.out.println("enter girlMoveBrick and gonna to wait***");
wait();
}
for (int i = 0; i < 2;) {
if (oddIndex >= records.length && evenIndex >= records.length) {//all 4 bricks had been checked
writer.flush();
return;
}
if (evenIndex >= records.length) {
break;
}
if (records[evenIndex] % 2 == 0) {
i++;
writer.print(records[evenIndex] + " ");
result[index++] = records[evenIndex];
writer.flush();
addCount();
}

evenIndex++;
System.out.println("girlMoveBrick &i=" + i + " &evenIndex=" + evenIndex);
if (oddIndex >= records.length && evenIndex >= records.length) {
System.out.println("this group finish,end with girl " );
}
}
ladyWaitFlag = true;
notify();
} catch (Exception ex) {
ex.printStackTrace();
}
}//end of while
}//end of girlMoveBrick

private synchronized static void addCount(){
finishCount++;
if(finishCount== 12){
System.out.println("Done!");
}
}
}

0 0