Commit 6b06b603 by bing.liu

asdsa

parent b16f7cb9
/**
* Copyright (C), 2015-2018, 非码网络科技有限公司
* FileName: Test
* Author: bing.liu
* Date: 2018-11-01 17:59
* Description:
*/
package cn.freemud.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
/**
* @author bing.liu
* @create 2018-11-01
* @versions 1.0.0
*/
public class Test {
private static int len = 5;
private static String IOtext1 = "io content-one";
private static String IOtext2 = "io content-tow";
@org.junit.Test
public void test1() throws IOException, InterruptedException {
mainAction();
}
public static void mainAction() throws IOException, InterruptedException {
HashMapAction();
ArrayListAction();
LinkedListAction();
IOAction();
new ThreadDemo("Thread-one").start();
new ThreadDemo("Thread-two").start();
Thread.sleep(1000);
new RunnableDemo("Runnable-one").start();
new RunnableDemo("Runnable-two").start();
}
public static void HashMapAction() {
HashMap<Integer, String> hashMap = new HashMap();
for (int i = 0; i < len; i++) {
hashMap.put(i, "hashMapValue".concat(String.valueOf(i)));
}
hashMap.forEach((k, v) -> System.out.println("hashMap Key:".concat(k.toString()).concat(",values:").concat(v)));
System.out.println();
}
public static void ArrayListAction() {
ArrayList arrayList = new ArrayList();
for (int i = 0; i < len; i++) {
arrayList.add("arrayList".concat(String.valueOf(i)));
}
arrayList.forEach((k) -> System.out.println("arrayList content:".concat(k.toString())));
System.out.println();
}
public static void LinkedListAction() {
LinkedList linkedList = new LinkedList();
for (int i = 0; i < len; i++) {
linkedList.add("linkedList".concat(String.valueOf(i)));
}
linkedList.forEach((k) -> System.out.println("linkedList content:".concat(k.toString())));
System.out.println();
}
public static void IOAction() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\FileOutputStream.txt");
fos.write(IOtext1.getBytes("UTF-8"));
fos.close();
FileInputStream fr = new FileInputStream("D:\\FileOutputStream.txt");
byte[] buf = new byte[1024];
int len;
String myStr = "";
while ((len = fr.read(buf)) != -1) {
myStr += new String(buf, 0, len, "UTF-8");
}
System.out.println(myStr);
FileWriter fw = new FileWriter("D:\\FileWriter.txt");
fw.write(IOtext2);
fw.close();
FileReader fr2 = new FileReader("D:\\FileWriter.txt");
char[] buf2 = new char[1024];
int len2 = fr2.read(buf2);
String myStr2 = new String(buf2, 0, len2);
System.out.println(myStr2);
System.out.println();
}
static class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo(String name) {
threadName = name;
}
public void run() {
System.out.println("Run " + threadName);
try {
for (int i = 0; i < 4; i++) {
System.out.println("Thread: " + threadName + ", " + i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " esc.");
System.out.println();
}
public void start() {
System.out.println("Star " + threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
static class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo(String name) {
threadName = name;
}
public void run() {
System.out.println("Run " + threadName);
try {
for (int i = 0; i < 4; i++) {
System.out.println("Thread: " + threadName + ", " + i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " esc.");
System.out.println();
}
public void start() {
System.out.println("Star " + threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment