首页 > 编程语言 > 详细

从头认识java-17.1 多线程

时间:2016-01-10 09:19:25      阅读:203      评论:0      收藏:0      [点我收藏+]

这一章节我们来讨论一下多线程。

1.什么是多线程?

多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程(台湾译作“执行绪”),进而提升整体处理性能。

说白了,一个线程就是一个任务,多线程就是程序分为多个任务并行运行


2.线程与进程的区别?

区别在于是否有共享变量。

进程有一套自己的变量,进程间不共享。

线程之间存在共享变量,共享IO、内存等资源

package com.ray.ch17;

import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class Test2 {
	public static void main(String[] args) throws IOException,
			InterruptedException {
		CountDownLatch countDownLatch = new CountDownLatch(2);
		FileWriter fileWriter = new FileWriter("d://123.txt");
		WriteTxtA writeTxtA = new WriteTxtA(fileWriter, countDownLatch);
		WriteTxtB writeTxtB = new WriteTxtB(fileWriter, countDownLatch);
		Thread threadA = new Thread(writeTxtA);
		Thread threadB = new Thread(writeTxtB);
		threadA.start();
		threadB.start();
		countDownLatch.await();
		fileWriter.close();
	}
}

class WriteTxtA implements Runnable {
	private FileWriter fileWriter;

	private CountDownLatch countDownLatch;

	public WriteTxtA(FileWriter fileWriter, CountDownLatch countDownLatch) {
		this.fileWriter = fileWriter;
		this.countDownLatch = countDownLatch;
	}

	@Override
	public void run() {
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println("WriteTxtA");
				fileWriter.write("WriteTxtA /n");
				Thread.sleep(50);
			}
			countDownLatch.countDown();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class WriteTxtB implements Runnable {
	private FileWriter fileWriter;

	private CountDownLatch countDownLatch;

	public WriteTxtB(FileWriter fileWriter, CountDownLatch countDownLatch) {
		this.fileWriter = fileWriter;
		this.countDownLatch = countDownLatch;
	}

	@Override
	public void run() {
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println("WriteTxtB");
				fileWriter.write("WriteTxtB /n");
				Thread.sleep(50);
			}
			countDownLatch.countDown();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

输出:

WriteTxtA
WriteTxtB
WriteTxtB
WriteTxtA
WriteTxtA
WriteTxtB
WriteTxtA
WriteTxtB
WriteTxtA
WriteTxtB

上面的例子就是共享 了FileWriter这个IO资源。


3.例子:

package com.ray.ch17;

public class Test2 {
	public static void main(String[] args) {
		PrintA printA = new PrintA();
		PrintB printB = new PrintB();
		Thread threadA = new Thread(printA);
		Thread threadB = new Thread(printB);
		threadA.start();
		threadB.start();
	}
}

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

class PrintB implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("PrintB");
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

输出:

PrintA
PrintB
PrintA
PrintB
PrintA
PrintB
PrintA
PrintB
PrintA
PrintB


4.实现多线程的过程

(1)实现Runnable接口

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


(2)创建一个类对象

PrintA printA = new PrintA();

(3)使用Runnable对象构造Thread对象

Thread threadA = new Thread(printA);

(4)运行start方法

threadA.start();


总结:这一章节介绍了线程的一些方面。


这一章节就到这里,谢谢。

-----------------------------------

目录




从头认识java-17.1 多线程

原文:http://blog.csdn.net/raylee2007/article/details/50491178

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!