多线程
所谓多线程,是一种并行处理技术。
多线程学习方法
菜鸟多线程
JAVA在线文档java.lang.Thread
上面的网站是个宝藏!!按上面的学,挺有用。
多线程程序一般步骤
声明一个线程private Thread t;
实例化一个线程t = new Thread (this, threadName);
应用一个线程t.start ();
结束一个线程t.yield();
package com.company;
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 Main {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
}
}