JAVA多线程-根据lock代码重写
来源:互联网 发布:淘宝汽车座垫套 编辑:程序博客网 时间:2024/06/10 20:26
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!");
}
}
}
- JAVA多线程-根据lock代码重写
- Java多线程---Lock类
- java多线程-Lock
- Java 多线程:Lock 关键字
- Java 多线程:Lock接口
- Java 多线程:Lock 关键字
- Java多线程--Lock接口
- Java多线程4- Lock、Condition
- Java多线程4- Lock、Condition
- Lock的使用 java多线程
- Java 锁LOCK&多线程安全
- JAVA Lock 多线程顺序执行
- Java多线程--Lock的使用
- java多线程:8、初用Lock
- java多线程Lock的使用。
- Java 多线程之Lock接口
- Java多线程(四)Lock锁
- java多线程之Lock介绍
- Sensor框架HAL层和Framework层解读
- Uri详解之——Uri结构与代码提取
- JAVA泛型的学习
- Eclipse中的智能提示
- timedelta
- JAVA多线程-根据lock代码重写
- 常用SQL语句(增删查改、合并统计、模糊搜索)
- 刷题总结#12
- Golang中的正则表达式
- cocos2d-x 提升篇 (20) SpriteFrameCache类的使用
- VS2012(InstallShield2015LimitedEdition)打包程序详解
- STM32F0的BOOT
- 怎样减少 Android 应用包 60% 的大小?
- https工作原理