JAVA Study Notes - common function module code

多线程

package pack1;

class RunnableDemo implements Runnable {
private Thread t;
private String threadName;

RunnableDemo(String name) {
threadName = name;
System.out.println("Creating " + threadName);
}

public void run() {
System.out.println("Running " + threadName);
try {
for (int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// 让线程睡眠一会
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
// System.out.println("Thread " + threadName + " exiting.");
}

public void start() {
// System.out.println("Starting " + threadName);
// if (t == null) {
t = new Thread(this, threadName);
t.start();
// }
}
}

public class TestThread {

public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo("Thread-1");
R1.start();

RunnableDemo R2 = new RunnableDemo("Thread-2");
R2.start();
}
}

文件写入内容
the java.io. * Import; 

public class the WriteFile {
public static void main (String [] args) {
the try {
BufferedWriter, new new BufferedWriter, OUT = (new new FileWriter ( "runoob.txt"));
        // if additional data, then the file later add a name parameter to true
out.write ( "novice tutorial");
the out.close ();
( "successfully created file!") System.out.println;
} the catch (IOException E) {
}
}
}

reading document content
import java.io.*;

public class WriteFile {
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new FileReader("test.log"));
String str;

while ((str = in.readLine()) != null) {
System.out.println(str);
}
System.out.println(str);
} catch (IOException e) {
}
}
}


More common module functions https://www.runoob.com/java/java-examples.html

Guess you like

Origin www.cnblogs.com/chenxiyuxiao/p/11577959.html