多线程文件操作
package javathread;
import java.io.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class outputstr extends Thread{
public int id;
public int index;
public String str;
public String filesname;
public Lock lock;
public int[][] num;
public outputstr(){
}
public void run(){
lock.lock();
try{
RandomAccessFile input=new RandomAccessFile(filesname, "rw");
for(int k=0;k<128;k++){
str+=((int)(Math.random()*26+'A'));
}
input.seek(index);
input.writeBytes(str);
input.close();
byte[] tmp=null;
tmp = str.getBytes("GB2312");
num[id][1]=tmp.length;
System.out.println("insert str "+id+":"+num[id][1]);
}catch(IOException e){
e.printStackTrace();
}
lock.unlock();
}
}
class inputstr extends Thread{
public int id;
public int index;
public int length;
public String str;
public String filesname;
public Lock lock;
public inputstr(){
}
public void run(){
lock.lock();
try{
RandomAccessFile output=new RandomAccessFile(filesname, "r");
byte[] tmp=new byte[length];
output.seek(index);
output.read(tmp);
str=new String(tmp);
System.out.println("read str "+id+":"+str.length());
output.close();
}catch(IOException e){
e.printStackTrace();
}
lock.unlock();
}
}
public class srandstr {
private static int[][] num;
private String filesname="004.txt";
private static Lock lock ;
public srandstr(){
}
public void creat8Gfiles(){
try{
FileWriter writer=new FileWriter(filesname);
BufferedWriter buffer=new BufferedWriter(writer);
StringBuilder[] str=new StringBuilder[20];
int time=128*1024;
System.out.printf("initialize:\n");
for(int j=0;j<20;j++){
str[j]=new StringBuilder();
for(int k=0;k<1024;k++){
str[j].append((int)(Math.random()*10));
}
}
System.out.printf("write in:\n");
long start=System.currentTimeMillis();
for(int j=0;j<64;j++){
for(int i=0;i<time;i++){
buffer.write(str[(int)(Math.random()*20)].toString());
}
System.out.printf(" %02d%%\n",j*100/64);
}
System.out.printf("100%%\n");
buffer.flush();
buffer.close();
long end=System.currentTimeMillis();
int sec =(int)(end-start)/1000%60;
int mine=(int)(end-start)/1000/60%60;
int hour=(int)(end-start)/1000/3600;
System.out.printf("finish:%02d:%02d:%02d\n",hour,mine,sec);
}catch(IOException e){
e.printStackTrace();
}
}
private boolean checksame(int n,int index){
for(int i=0;i<index;i++){
if(n==num[i][0]){
return true;
}
}
return false;
}
public void insertstr(){
try{
System.out.println("insert :");
RandomAccessFile input=new RandomAccessFile("004.txt", "rw");
long maxid = input.length()/128;
input.close();
outputstr[] tmp = new outputstr[100];
for(int j=0;j<100;j++){
for(;checksame(num[j][0],j);){
num[j][0]=(int)(Math.random()*maxid);
}
tmp[j]=new outputstr();
tmp[j].lock=lock;
tmp[j].num=num;
tmp[j].filesname=filesname;
tmp[j].id=j;
tmp[j].index=num[j][0];
}
for(int j=0;j<100;j++){
tmp[j].start();
}
for(outputstr me:tmp){
me.join();
}
System.out.println("insert end:");
}catch(IOException | InterruptedException e){
e.printStackTrace();
}
}
public void readstr(){
inputstr[] tmp=new inputstr[100];
System.out.println("read :");
for(int j=0;j<100;j++){
tmp[j]=new inputstr();
tmp[j].lock=lock;
tmp[j].id=j;
tmp[j].index=num[j][0];
tmp[j].length=num[j][1];
tmp[j].filesname=filesname;
}
for(int j=0;j<100;j++){
tmp[j].start();
}
for(inputstr me:tmp){
try {
me.join();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
System.out.println("read end:");
}
public static void main(String[] args) {
lock = new ReentrantLock();
num=new int[100][2];
srandstr tmp=new srandstr();
tmp.creat8Gfiles();
tmp.insertstr();
tmp.readstr();
}
}
生产者消费者问题
//商品
class commo{
private int num;
public commo(){
num=0;
}
public synchronized int getcommo(){
System.out.printf("commo:%d ",num);
if(num>0){
num--;
return 1;
}
return 0;
}
public synchronized int procommon(){
System.out.printf("commo:%d ",num);
if(num<10){
num++;
return 1;
}
return 0;
}
}
//生产者
class producer extends Thread{
private int id;
private commo tmp;
public producer(int id,commo tmp){
this.id=id;
this.tmp=tmp;
}
public void run(){
try {
for(;;Thread.sleep(1000)){
if(tmp.procommon()==0){
System.out.printf("[%d] no pro\n",id);
Thread.yield();
}
else System.out.printf("[%d] pro\n",id);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//消费者
class consumer extends Thread{
private int id;
private commo tmp;
public consumer(int id,commo tmp){
this.id=id;
this.tmp=tmp;
}
public void run(){
try {
for(;;Thread.sleep(1000)){
if(tmp.getcommo()==0){
System.out.printf("[%d] no get\n",id);
Thread.yield();
}
else System.out.printf("[%d] get\n",id);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class produandconsu {
public static void main(String[] args) {
commo com=new commo();
consumer[] tmp=new consumer[5];
for(int i=0;i<5;i++){
tmp[i]=new consumer(i,com);
}
producer[] tmp2=new producer[10];
for(int i=0;i<10;i++){
tmp2[i]=new producer(i,com);
}
for(int i=0;i<5;i++){
tmp[i].start();
}
for(int i=0;i<10;i++){
tmp2[i].start();
}
}
}