Java的一些高级特性(八)——Java7中的线程

我们先来看看一个简单的线程:

一个新的线程都要继承Thread类,并且将工作内容放到run()方法中:

package com.freesoft.testentity;

public class WorkingThread extends Thread{
	@Override
	public void run() {
		int iterations = 5;
		try {
			for (int i = 0; i < iterations; i++) {
				System.out.println("From working Thread...");
					Thread.sleep(2000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

我们的主线程的主要工作处于main()方法中,启动工作线程的方法是使用workingThread的start()方法:

package com.freesoft.java7newfeature;

import com.freesoft.testentity.WorkingThread;

public class TestWorkingThread {
	public static void main(String[] args) {
		int iterations = 3;
		WorkingThread thread = new WorkingThread();
		thread.start();
		
		try {
			for (int i = 0; i < iterations; i++) {
				System.out.println("From main Thread...");
					Thread.sleep(2000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

另一个实现线程的方法是使我们的工作线程实现Runnable接口:

package com.freesoft.testentity;

public class MyWorkingRunnable implements Runnable {
	@Override
	public void run() {
		int iterations = 5;
		try {
			for (int i = 0; i < iterations; i++) {
				System.out.println("From runnable Thread...");
					Thread.sleep(2000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

而使用Runnable对象的地方使用如下代码,也就是Thread的构造函数中将runnable对象传入:

package com.freesoft.java7newfeature;

import com.freesoft.testentity.WorkingRunnable;
import com.freesoft.testentity.WorkingThread;

public class TestWorkingThread {
	public static void main(String[] args) {
		int iterations = 3;
		WorkingThread thread = new WorkingThread();
		thread.start();
		WorkingRunnable runnable = new WorkingRunnable();
		new Thread(runnable).start();
		
		try {
			for (int i = 0; i < iterations; i++) {
				System.out.println("From main Thread...");
					Thread.sleep(2000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

一般情况下我们不会主动去打断一个线程,除非必要,我们都是在线程的run()方法中判断一定条件使得我们可以正常的退出线程;如果确实必要,我们使用Thread的interrupt()方法来终止线程继续执行。


最后一个最重要的内容是线程的同步。我们考虑一个简单的情况:几个线程属于同一个线程类,大家共享同一个lock对象,这样我们就可以使用synchronized关键字来处理:

我们建立一个Lock类用来同步:

package com.freesoft.testentity;

public class ThreadLock {
	
}

接着我们在工作线程中同步这个lock对象:

package com.freesoft.testentity;

public class WorkingThread extends Thread {
	private ThreadLock lock;
	private int threadId;

	public WorkingThread(ThreadLock lock, int threadId) {
		super();
		this.lock = lock;
		this.threadId = threadId;
	}

	@Override
	public void run() {
		synchronized (lock) {
			try {
					Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("Lock will unlock from thread " + threadId);
		}
	}
}

最后我们的测试代码如下:

package com.freesoft.java7newfeature;

import com.freesoft.testentity.ThreadLock;
import com.freesoft.testentity.WorkingThread;

public class TestWorkingThread {
	public static void main(String[] args) {
		ThreadLock lock = new ThreadLock();
		WorkingThread t1 = new WorkingThread(lock, 1);
		WorkingThread t2 = new WorkingThread(lock, 2);
		WorkingThread t3 = new WorkingThread(lock, 3);
		
		t1.start();
		t2.start();
		t3.start();
	}

}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值