62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*;
import java.nio.channels.WritableByteChannel;
import java.nio.ByteBuffer;
public class Test{
public static void main(String[] args){
String[] strs = {"A","B","C","D"};//设置写入的内容.可以无限添加长度和调整顺序
Path file = Paths.get(System.getProperty("user.dir")).resolve("A.txt");
int length = strs.length;
int time = 100;//这里设置每个内容写入的次数.
Group group = new Group(file,strs);
Thread[] threads = new Thread[length];
for(int i = 0 ; i < length ; i ++){
threads[i] = new Thread(new Task(group,i,time));
}
for(int i = 0 ; i < length ; i ++){
threads[i].start();
}
}
}
class Group{
public Group(Path file,String ... contents){
assert (Files.notExists(file) || Files.isRegularFile(file));
try{
channel = Files.newByteChannel(file,CREATE,WRITE,APPEND);
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
this.length = contents.length;
conditions = new Condition[length];
this.contents = contents;
for(int i = 0 ; i < length ; i ++){
conditions[i] = lock.newCondition();
}
lives = new boolean[length];
lives[0] = true;
}
public void process(int index){
if(firstTimeWait){//首次让第一个线程睡眠,腾出时间让第二个线程启动.
firstTimeWait = false;
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
lock.lock();
assert index >= 0 && index < length;
if(!lives[index]){
conditions[index].await();
}
lives[index] = false;
try{
Thread.sleep(10);//这里需要设置等待.要不有时候处理太快会出现死锁.
}catch(InterruptedException e){
e.printStackTrace();
}
channel.write(ByteBuffer.wrap(contents[index].getBytes()));
if(index == length - 1){
conditions[0].signalAll();
}else{
conditions[index + 1].signalAll();
}
lock.unlock();
}catch(InterruptedException|IOException e){
e.printStackTrace();
System.exit(1);
}
}
public int getLength(){
return length;
}
private final ReentrantLock lock = new ReentrantLock();
private final Condition[] conditions;
private boolean[] lives;
private final String[] contents;
private final int length;
private WritableByteChannel channel;
private boolean firstTimeWait = true;
}
class Task implements Runnable{
public Task(Group group,int index,int time){
this.group = group;
this.index = index;
assert index < group.getLength();
this.time = time;
}
@Override
public void run(){
for(int i = 0 ; i < time ; i ++){
group.process(index);
if(i == time - 1){
Thread.currentThread().interrupt();
}
}
}
private Group group;
private int index;
private int time;
}
public class TestThread
{
// private static final Log LOGGER = LogFactory.getLog(TestThread.class);
public static void main(String[] args)
{
Runnable runnableA = new Runnable()
{
public void run()
{
int i = 0;
while (i < 10)
{
writeA();
writeB();
writeC();
i++;
}
System.out.println();
}
};
Runnable runnableB = new Runnable()
{
public void run()
{
int i = 0;
while (i < 10)
{
writeA();
writeB();
writeC();
i++;
}
System.out.println();
}
};
Runnable runnableC = new Runnable()
{
public void run()
{
int i = 0;
while (i < 10)
{
writeA();
writeB();
writeC();
i++;
}
System.out.println();
}
};
Thread threadA = new Thread(runnableA);
Thread threadB = new Thread(runnableB);
Thread threadC = new Thread(runnableC);
threadA.start();
threadB.start();
threadC.start();
}
static String flagA = "A";
static String flagB = "B";
static String flagC = "C";
private static void writeA()
{
ReentrantLock lock = new ReentrantLock();
try
{
lock.lock();
if (flagA.equals("A"))
{
System.out.print("A");
flagA = "B";
}
} finally
{
lock.unlock();
}
}
private static void writeB()
{
ReentrantLock lock = new ReentrantLock();
try
{
lock.lock();
if (flagB.equals("B"))
{
System.out.print("B");
flagA = "C";
}
} finally
{
lock.unlock();
}
}
private static void writeC()
{
ReentrantLock lock = new ReentrantLock();
try
{
lock.lock();
if (flagC.equals("C"))
{
System.out.print("C");
flagA = "A";
}
} finally
{
lock.unlock();
}
}
}
package com;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Problem {
public void appendMethod(String fileName, String content) {
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String A = "A";
String B = "B";
String C = "C";
String D = "D";
String[] contexts = new String[4];
contexts[0] = A;
contexts[1] = B;
contexts[2] = C;
contexts[3] = D;
String pre = "E:\\thread\\";
String suf = ".txt";
String[] fileNames = new String[4];
List<ArrayBlockingQueue<String>> queues = new ArrayList<ArrayBlockingQueue<String>>();
//数组有泛型问题,不好用。
// ArrayBlockingQueue<?>[] queues = new ArrayBlockingQueue<?>[4];
for (int i = 0; i < contexts.length; i++) {
String fileName = pre + contexts[i] + suf;
fileNames[i] = fileName;
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(
contexts.length);
try {
queue.put(fileName);
} catch (InterruptedException e) {
}
queues.add(queue);
}
for (int i = 0; i < contexts.length; i++) {
ArrayBlockingQueue<String> producet;
if ((i + 1) == contexts.length) {
producet = queues.get(0);
} else {
producet = queues.get(i + 1);
}
new Thread(new R(contexts[i], producet, queues.get(i))).start();
}
}
}
class R implements Runnable {
String context;
BlockingQueue<String> producet;
BlockingQueue<String> consumer;
public R(String context, BlockingQueue<String> producet,
BlockingQueue<String> consumer) {
this.context = context;
this.producet = producet;
this.consumer = consumer;
}
public void run() {
try {
Problem p = new Problem();
for (int i = 0; i < 100; i++) {
String fileName = consumer.take();
p.appendMethod(fileName, context);
producet.put(fileName);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
java.util.concurrent.CountDownLatch