您的位置:首页 > 编程语言 > Java开发

java线程简析

2015-11-01 10:35 281 查看
线程简析:

线程是正在运行程序进程中单一的执行流,一个进程对应一个程序,一个程序可以有多个进程,进程有自己的独立空间,线程只能共享进程中的空间,一个进程至少包含一个线程。线程最大的特点是可以同时并发执行多个任务。

比如程序要执行煮饭、洗菜、切菜、炒菜、吃饭五个方法,煮饭要20分钟,洗菜15分钟,切菜5分钟,炒菜15分钟,最后吃饭。在不使用线程时,程序执行这五个方法是窜起来,一个方法接一个方法去完成,总共要花20+15+5+15=55分钟,而使用线程时,程序的运行是这样的:在煮饭的时候CPU会有休眠的时间,这时CPU就会执行下一个方法即洗菜,洗菜的时候可能在接水时需要等待,那么CPU右可以执行下一个方法,按照这样的逻辑执行,这时程序的执行这五个方法就像人执行煮饭、洗菜、切菜、炒菜、吃饭这五个过程一样,合理和最大效率的完成这五件事情,也就是说使CPU的闲置时间减少,从而提高效率,节省时间。

实现多线程的方法:

继承Thread类

<pre name="code" class="java">public class MyThread extends Thread{
//重写run方法
public void run(){
//要做的事
}
}

实现Runnable接口
public class MyThread implements Runnable{
//重写run方法
public void run(){
//要做的事
}
}

下面用具体代码体现:
public class Man {
public static void main(String[] args) {
Women men = new Women();
//煮饭:启动煮饭线程
CookieThread ct = new CookieThread();
ct.start();//启动线程
//洗菜
WashThread wt = new WashThread();
wt.start();
//切菜
CutThread cut = new CutThread();//创建线程对象
Thread cutThread = new Thread(cut);//把线程对象包装成Thread对象
cutThread.start();
//炒菜
FireThread fire = new FireThread();//创建线程对象
Thread fireThread = new Thread(fire);//把线程对象包装成Thread对象
fireThread.start();
//吃饭
men.eat();
}
public void eat(){
System.out.println("开始吃饭了!");
}
}

//煮饭类的线程
public class CookieThread extends Thread{
public void run() {
cookie();
}
/**
* 煮饭
*/
public void cookie(){
System.out.println("开始煮饭.....");
try {
//休眠2秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("煮饭完成!");
}
}

//洗菜类的线程
public class WashThread extends Thread{
public void run() {
wash();
}
/**
* 洗菜
*/
public void wash(){
System.out.println("开始洗菜.....");
try {
//休眠2秒
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("洗菜完成!");
}
}

//切菜类的线程
public class CutThread implements Runnable{
public void run() {
cut();
}
/**
* 切菜
*/
public void cut(){
System.out.println("开始切菜.....");
try {
//休眠2秒
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("切菜完成!");
}
}

//炒菜类的线程
public class FireThread implements Runnable{
public void run() {
fire();
}
/**
* 炒菜
*/
public void fire(){
System.out.println("开始炒菜.....");
try {
//休眠2秒
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("炒菜完成!");
}
}


Synchronized同步锁关键字、wait()方法、notify()方法:

用生产/消费模型说明:

public class Producter extends Thread{
private ArrayList<Iphone> list;
public Producter(ArrayList<Iphone> list){
this.list = list;
}

public void run() {
//定义一个计数器,来标志手机序列号
int count = 0;
synchronized (list) {
while(true){
//判断当前集合中是否还有手机
if(list.size()>0){
//说明集合中还有手机,不应该生产
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//生产手机
Iphone ip = new Iphone("苹果5S"+count);
count++;
list.add(ip);
System.out.println("生产商生产了一个手机,手机型号为:"+ip.name);
//通知消费者,不要再等待了,可以消费手机
list.notify();
}
}
}
}




Synchronized同步锁关键字、wait()方法、notify()方法:

用生产/消费模型说明:

public class Producter extends Thread{
private ArrayList<Iphone> list;
public Producter(ArrayList<Iphone> list){
this.list = list;
}

public void run() {
//定义一个计数器,来标志手机序列号
int count = 0;
synchronized (list) {
while(true){
//判断当前集合中是否还有手机
if(list.size()>0){
//说明集合中还有手机,不应该生产
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//生产手机
Iphone ip = new Iphone("苹果5S"+count);
count++;
list.add(ip);
System.out.println("生产商生产了一个手机,手机型号为:"+ip.name);
//通知消费者,不要再等待了,可以消费手机
list.notify();
}
}
}
}

public class Customer extends Thread{
private ArrayList<Iphone> list;
public Customer(ArrayList<Iphone> list){
this.list = list;
}
public void run() {
super.run();
synchronized (list) {
while(true){
//访问仓库中的数据是否还有
if(list.size()==0){
//处于等待
try {
list.wait();//等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//买手机
Iphone ip = list.remove(0);
//输出手机的型号
System.out.println("买了一个手机,型号为:"+ip.name);
//告诉生产者,你可以生产手机
list.notify();//通知
}
}
}
}

public class Iphone {
//定义属性
public String name;
public Iphone(String name){
this.name = name;
}
}

public class Test {
public static void main(String[] args) {
//定义一个集合仓库,用来存储共同访问的手机
ArrayList<Iphone> list = new ArrayList<Iphone>();
//创建生产线程
Producter product = new Producter(list);
//创建消费线程
Customer customer = new Customer(list);
//运行线程
product.start();
customer.start();
}
}
运行结果为:
生产商生产了一个手机,手机型号为:苹果5S84620

买了一个手机,型号为:苹果5S84620

生产商生产了一个手机,手机型号为:苹果5S84621

买了一个手机,型号为:苹果5S84621

生产商生产了一个手机,手机型号为:苹果5S84622

买了一个手机,型号为:苹果5S84622

。。。。。。。。。

线程池:

线程的生命周期:



线程状态:



比如现在公司有五名员工,公司有一个任务列表,需要十名员工一起完成所有任务:

这五名员工是每个人随机获得一个任务,而且每名员工几乎是同时去获取完成任务

public class ThreadPool {
//先定义一个容器,用来存储执行线程
private PoolWorker[] workerArray = new PoolWorker[10];
//定义一个容器,用来存储任务
private ArrayList<MyTask> taskList = new ArrayList<MyTask>();

public ThreadPool(){
//预置好所有的执行线程
for(int i=0;i<workerArray.length;i++){
PoolWorker worker = new PoolWorker(taskList);
worker.setName("第"+i+"号员工");
workerArray[i] = worker;
worker.start();//启动线程
System.out.println(worker.getName()+"已经准备完毕,等待执行任务!");
}
System.out.println("******************线程池预置完毕**************************");
}
//分配任务到任务列表
public void excute(MyTask task){
synchronized (taskList) {
taskList.add(task);
//通知所有等待的线程,可以取任务了
taskList.notify();
}
}

public static void main(String[] args) {
//1.先创建公司(线程池)
ThreadPool pool = new ThreadPool();
//分配任务
for(int i=0;i<60;i++){
MyTask task = new MyTask(i);
pool.excute(task);
}
}
}

public class PoolWorker extends Thread{
//所有工作线程共享同一份任务列表
private ArrayList<MyTask> taskList;
public PoolWorker(ArrayList<MyTask> taskList){
this.taskList = taskList;
}
@Override
public void run() {
while(true){
//同步关键字
synchronized (taskList) {
if(taskList.isEmpty()){
try {
taskList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//取出一个任务,并执行
MyTask task = taskList.remove(0);
System.out.println(this.getName()+"开始执行"+task.index+"任务");
//执行任务
task.work();
}
}
}

public class MyTask {
//任务编号
public int index;
public MyTask(int index){
this.index = index;
}
public void work(){
System.out.println(index+"号任务开始....");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(index+"号任务结束!");
}
}
输出结果为:

第0号员工已经准备完毕,等待执行任务!

第1号员工已经准备完毕,等待执行任务!

第2号员工已经准备完毕,等待执行任务!

第3号员工已经准备完毕,等待执行任务!

第4号员工已经准备完毕,等待执行任务!

******************线程池预置完毕**************************

第0号员工开始执行1任务

第3号员工开始执行3任务

第2号员工开始执行2任务

2号任务开始....

第4号员工开始执行1任务

1号任务开始....

3号任务开始....

1号任务开始....

第1号员工开始执行5任务

5号任务开始....

2号任务结束!

1号任务结束!

第2号员工开始执行5任务

5号任务开始....

3号任务结束!

5号任务结束!

1号任务结束!

5号任务结束!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: