启动一个线程是用run() 还是start()

启动一个线程是调用start()方法,使线程就绪状态,以后可以被调度为运行状态,一个线程必须关联一些具体的执行代码,run()方法是该线程所关联的执行代码。[code]package net.hawebs.oss;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**

*/
public class Hawebs {

private int j;
private final Lock lock = new ReentrantLock();

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Hawebs tt = new Hawebs();
	for (int i = 0; i < 2; i++) {
		new Thread(tt.new Adder()).start();
		new Thread(tt.new Subtractor()).start();
	}
}

private class Subtractor implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			/*
			 * synchronized (ThreadTest.this) { System.out.println("j--=" +
			 * j--); //这里抛异常了,锁能释放吗? }
			 */
			lock.lock();
			try {
				System.out.println("j--=" + j--);
			} finally {
				lock.unlock();
			}
		}
	}

}

private class Adder implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			/*
			 * synchronized (ThreadTest.this) { System.out.println("j++=" +
			 * j++); }
			 */
			lock.lock();
			try {
				System.out.println("j++=" + j++);
			} finally {
				lock.unlock();
			}
		}
	}

}

}
[/code]