JAVA锁-Lock-对格式互答
来源:互联网 发布:博图软件下载 编辑:程序博客网 时间:2024/06/05 16:10
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;
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.startBuck();
}
}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 startBuck(){
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;
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.startBuck();
}
}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 startBuck(){
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-对格式互答
- Java Concurrent Lock锁
- java并发 lock锁
- java Lock(显示锁)
- Java lock锁
- Java中的Lock锁
- Java中的Lock锁
- Java中的锁-Lock
- Java学习Lock.lock()
- Java 锁LOCK&多线程安全
- Java NIO-锁机制Lock
- Java锁----Lock实现原理
- java 线程七-Lock锁
- java并发锁-Lock接口
- JAVA并发编程-LOCK锁
- java中的锁-Lock-ReentrantLock
- java并发编程---lock锁
- Java锁----Lock实现原理
- 存储过程各个版本
- mysql解压缩
- Abbott's Revenge UVA
- Linux查找含有某字符串的所有文件
- windows下,安装python的chardet
- JAVA锁-Lock-对格式互答
- Markdown使用小笔记
- PAT_乙级_1012_筱筱
- Activity 和 Intent
- Java NIO系列教程(八) SocketChannel
- Live555源代码下载编译与测试
- 扫地机器人常见的5种定位技术解析
- 装饰者模式
- Image to Image图像自动配准