类别 | 方法 | 简介 |
线程的创建
|
Thread() | |
Thread(String name) | ||
Thread(Runable target) | ||
Thread(Runable target,String name) | ||
线程的方法
|
void start() | 启动线程 |
static void sleep(long millis) |
线程休眠
|
|
static void sleep(long millis,int nanos) | ||
void join() |
使其他线程等待当前线程终止
|
|
void join(long millis) | ||
void join(long millis,int nanos) | ||
static void yield() | 当前运行线程释放处理器资源 | |
获取线程的引用 | static Thread currentThread() | 返回当前运行的线程引用 |
package com.qunar.thread;
public class RunableDemo implements Runnable{
@Override
public void run() {
while(Thread.currentThread().isInterrupted()){
//
}//while
}
}
package com.qunar.thread;
public class ThreadDemo extends Thread{
@Override
public void run() {
while(isInterrupted()){
}//while
}
}
package com.qunar.thread;
public class TicketThread extends Thread{
@Override
public void run() {
while(true){
System.out.println("thread is running...");
long time = System.currentTimeMillis();
// 相当于sleep(1000)
while(System.currentTimeMillis() - time < 1000){}
}//while
}
}
package com.qunar.test;
import com.qunar.thread.TicketThread;
public class ThreadTest {
public static void main(String[] args){
TicketThread ticketThread = new TicketThread();
System.out.println("start thread...");
ticketThread.start();
// 主线程休眠3秒
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 3秒后中断子线程
System.out.println("interrupted thread...");
ticketThread.interrupt();
// 主线程休眠3秒
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 主线程结束
System.out.println("stop thread...");
}
}
start thread...
thread is running...
thread is running...
thread is running...
thread is running...
interrupted thread...
thread is running...
thread is running...
thread is running...
stop thread...
thread is running...
thread is running...
thread is running...
thread is running...
thread is running...
package com.qunar.thread;
public class TicketThread extends Thread{
@Override
public void run() {
while(!isInterrupted()){
System.out.println("thread is running...");
long time = System.currentTimeMillis();
// 相当于sleep(1000)
while(System.currentTimeMillis() - time < 1000){}
}//while
}
}
start thread...
thread is running...
thread is running...
thread is running...
thread is running...
interrupted thread...
stop thread...
package com.qunar.thread;
public class TicketThread extends Thread{
@Override
public void run() {
while(!isInterrupted()){
System.out.println("thread is running...");
// 线程被阻塞1秒钟
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}//while
}
}
start thread...
thread is running...
thread is running...
thread is running...
interrupted thread...
thread is running...
java.lang.InterruptedException: sleep interrupted
thread is running...
at java.lang.Thread.sleep(Native Method)
at com.qunar.thread.TicketThread.run(TicketThread.java:11)
thread is running...
thread is running...
stop thread...
thread is running...
thread is running...
thread is running...
thread is running...
public void run(){
try{
...
while(more work to do){
do more work
sleep(delay);
}
}
catch(InterruptedException e){
// thread was interrupted during sleep
}
finally{
// cleanup if required
}
}
如果目前该线程被一个sleep(long),sleep(long,int),join(),join(long),join(long,int),wait(),wait(long), wait(long,int)调 用阻塞,那么,中断状态将被清空,同时InterruptedException异常被抛出。
注意:这是一个静态方法。这一调用会产生副作用-它将当前线程的中断状态重置为false。
|
accounts[i] += amount;
private Lock bankLock = new ReentrantLock();
...
// 转移
public void transfer(int from,int to,double amount){
if(accounts[from] < amount){
return;
}//if
// 加锁
bankLock.lock();
try{
System.out.print(Thread.currentThread().getName());
accounts[from] -= amount;
System.out.printf(" %10.2f from %d to %d",amount,from,to);
accounts[to] += amount;
System.out.printf(" Total Balance: %10.2f%n",GetTotalBalance());
}
finally{
// 解锁
bankLock.unlock();
}
}
每一个Bank对象有自己的ReentrantLock对象。 如果两个线程试图访问同一个Bank对象,那么锁以串行方式提供服务。 如果两个线程访问不同的Bank对象,每一个线程得到不同的锁对象,两个线程都不会发生阻塞。 要留心临界区中代码,不要因为异常的抛出而跳出了临界区。如果在临界区代码结束之前抛出了异常,finally子句释放锁,但会使对象处于一种受损的状态。 |