笔记第20天:
代码:
共三个类:
第一个类:
package com.qf.work;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* @author ken
*
*/
public class Client {
public static void main(String[] args) {
//定义一个锁对象
Object lock = new Object();
//创建4条线程
FileThread f1 = new FileThread("线程1", "A", lock);
FileThread f2 = new FileThread("线程2", "B", lock);
FileThread f3 = new FileThread("线程3", "C", lock);
FileThread f4 = new FileThread("线程4", "D", lock);
f1.start();
f2.start();
f3.start();
f4.start();
//构建一个map集合,方便后续通过字母找到相应的线程对象
Map<String, FileThread> threadMap = new HashMap<>();
threadMap.put("A", f1);
threadMap.put("B", f2);
threadMap.put("C", f3);
threadMap.put("D", f4);
//构建一个文件位置和文件内容的映射map
//C:\\file1.txt -> ABCDABCD
Map<String, String> fileMap = new HashMap<>();
fileMap.put("C:\\A\\file1.txt", "AAAA");
fileMap.put("C:\\A\\file2.txt", "ABABA");
fileMap.put("C:\\A\\file3.txt", "ACBC");
fileMap.put("C:\\A\\file4.txt", "DDCC");
synchronized (lock) {
//主线程开始调配工作
for(Entry<String, String> entry : fileMap.entrySet()){
//确定当前需要写入的文件路径
String path = entry.getKey();
//当前这个文件需要写入的内容
String content = entry.getValue();
//拆分内容 "ABC" -> ['A', 'B', 'C']
char[] contentArray = content.toCharArray();
for(char c : contentArray){//'A' -> "A"
//找到需要写入c这个内容的线程t
FileThread t = threadMap.get(c + "");
t.setPath(path);
//唤醒所有线程
lock.notifyAll();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//强制终止整个程序
System.exit(0);
}
}
}
第二个类:
package com.qf.work;
/**
* 文件线程
* @author ken
*
*/
public class FileThread extends Thread{
//当前线程能够写入的内容
private String content;
//当前内容写入何处
private String path;
//同步锁对象
private Object lock;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//
public FileThread(String threadName, String content, Object lock){
super(threadName);
this.content = content;
this.lock = lock;
}
//线程体
@Override
public void run() {
while(true){
synchronized (lock) {
if(path != null){
//xxxxxx
System.out.println(this.getName() + "需要往" + path + "文件中写入" + content);
//进行文件内容追加
FileUtil.contentAppend(content, path);
//将path制空
path = null;
//唤醒主线程重新分配path路径
lock.notifyAll();
} else {
System.out.println(this.getName() + "没事可做,去休息了!");
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
第三个类:
package com.qf.work;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileUtil {
/**
* 文件内容追加
*/
public static void contentAppend(String content, String path){
File file = new File(path);
File pfile = file.getParentFile();
if(!pfile.exists()){
pfile.mkdirs();
}
PrintWriter print = null;
try {
print = new PrintWriter(new FileWriter(file, true));
print.print(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(print != null){
print.close();
}
}
}
}
代码:
共三个类:
第一个类:
package com.qf.work;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* @author ken
*
*/
public class Client {
public static void main(String[] args) {
//定义一个锁对象
Object lock = new Object();
//创建4条线程
FileThread f1 = new FileThread("线程1", "A", lock);
FileThread f2 = new FileThread("线程2", "B", lock);
FileThread f3 = new FileThread("线程3", "C", lock);
FileThread f4 = new FileThread("线程4", "D", lock);
f1.start();
f2.start();
f3.start();
f4.start();
//构建一个map集合,方便后续通过字母找到相应的线程对象
Map<String, FileThread> threadMap = new HashMap<>();
threadMap.put("A", f1);
threadMap.put("B", f2);
threadMap.put("C", f3);
threadMap.put("D", f4);
//构建一个文件位置和文件内容的映射map
//C:\\file1.txt -> ABCDABCD
Map<String, String> fileMap = new HashMap<>();
fileMap.put("C:\\A\\file1.txt", "AAAA");
fileMap.put("C:\\A\\file2.txt", "ABABA");
fileMap.put("C:\\A\\file3.txt", "ACBC");
fileMap.put("C:\\A\\file4.txt", "DDCC");
synchronized (lock) {
//主线程开始调配工作
for(Entry<String, String> entry : fileMap.entrySet()){
//确定当前需要写入的文件路径
String path = entry.getKey();
//当前这个文件需要写入的内容
String content = entry.getValue();
//拆分内容 "ABC" -> ['A', 'B', 'C']
char[] contentArray = content.toCharArray();
for(char c : contentArray){//'A' -> "A"
//找到需要写入c这个内容的线程t
FileThread t = threadMap.get(c + "");
t.setPath(path);
//唤醒所有线程
lock.notifyAll();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//强制终止整个程序
System.exit(0);
}
}
}
第二个类:
package com.qf.work;
/**
* 文件线程
* @author ken
*
*/
public class FileThread extends Thread{
//当前线程能够写入的内容
private String content;
//当前内容写入何处
private String path;
//同步锁对象
private Object lock;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//
public FileThread(String threadName, String content, Object lock){
super(threadName);
this.content = content;
this.lock = lock;
}
//线程体
@Override
public void run() {
while(true){
synchronized (lock) {
if(path != null){
//xxxxxx
System.out.println(this.getName() + "需要往" + path + "文件中写入" + content);
//进行文件内容追加
FileUtil.contentAppend(content, path);
//将path制空
path = null;
//唤醒主线程重新分配path路径
lock.notifyAll();
} else {
System.out.println(this.getName() + "没事可做,去休息了!");
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
第三个类:
package com.qf.work;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileUtil {
/**
* 文件内容追加
*/
public static void contentAppend(String content, String path){
File file = new File(path);
File pfile = file.getParentFile();
if(!pfile.exists()){
pfile.mkdirs();
}
PrintWriter print = null;
try {
print = new PrintWriter(new FileWriter(file, true));
print.print(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(print != null){
print.close();
}
}
}
}