多线程Thread
线程(thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。
线程是独立调度和分派的基本单位。线程可以操作系统内核调度的内核线程。
同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间,文件描述符和信号处理等等。但同一进程中的多个线程有各自的调用栈(call stack),自己的寄存器环境(register context),自己的线程本地存储(thread-local storage)。
一个进程可以有很多线程,每条线程并行执行不同的任务。
科普已简化,详细自wiki维基百科
创建多线程
听了这么多多线程的概念,相信在座的各位“垃圾们”肯定已经跃跃欲试了。所以呢,如何创建一个多线程呢?
创建多线程有俩种方法:
- 通过创建
Thread
子类来实现 - 通过定义一个实现
Runnable
接口的类
extendsThread法
class extdsMultiThread extends Thread{
int sleepTime;
extdsMultiThread(String id){
super(id);
this.sleepTime = (int)(Math.random()*1000);
System.out.println("Thread's Name: "+this.getName()
+", sleep in: "+this.sleepTime+" ms.");
}
public void run(){
//Whichever realizing runnable interface or
//extending Thread class must set up run()
try{
Thread.sleep(this.sleepTime);
}catch(InterruptedException e){
System.err.println("Program breaks down: "+e.toString());
}
System.out.println("The thread running now is: "+this.getName());
}
}
public class extendsMultiThread {
public static void main(String[] AUG){
extdsMultiThread t_1, t_2, t_3, t_4;
t_1 = new extdsMultiThread("No.1");
t_2 = new extdsMultiThread("No.2");
t_3 = new extdsMultiThread("No.3");
t_4 = new extdsMultiThread("No.4");
t_1.start();
t_2.start();
t_3.start();
t_4.start();
}
}
运行实例:
因为每次休眠时间不一样,所以每次运行都是不一样
implementsRunnable法
class impleMultiThread implements Runnable{
int num;
impleMultiThread(int n){
this.num = n;
}
public void run(){
for(int i = 0; i < 2; i++)// Each thread runs twice
System.out.println("Running thread: "+this.num);
System.out.println("# Ending the Num: "+this.num);
}
}
public class runnableMultiThread {
public static void main(String[] AUG) throws InterruptedException{
Thread mt_1 = new Thread(new impleMultiThread(1));
Thread mt_2 = new Thread(new impleMultiThread(2));
Thread mt_3 = new Thread(new impleMultiThread(3));
mt_1.start();
mt_2.start();
mt_3.start();
// wait for the death of Thread
mt_1.join();
mt_2.join();
mt_3.join();
}
}
运行实例:
因为多线程在每台计算机,应该说每台不一样配置的设备上运行都是不同的
同步执行
当我们需要一条线程一条线程的执行怎么办?那就需要使用到synchronized
关键字,使当前执行的对象锁定或者说进入了监视器(monitor)。所以在同一时刻里面,只能有一个线程执行,其他线程则需要等待其结束后才可以执行。
其语法格式如下:
锁定对象(object)
synchronized (Object){
/*
Block //需要执行的语句体
*/
}
锁定方法(method)
synchronized void Function(para){
/*
Block // 语句体
*/
}
此外不可以、不可以、不可以对构造函数锁定。
资源冲突实例
- 未锁定
class thread{
void play(int n){
System.out.println("Running Thread No: "+n);
try{
Thread.sleep(3);
}catch(InterruptedException e){
System.out.println("Thread No: "+ n + " Error!");
}
System.out.println("# Ending Thread No: "+n);
}
}
class threadTest implements Runnable{
thread obj;
int num;
threadTest(thread o, int n){
this.obj = o;
this.num = n;
}
public void run(){
this.obj.play(this.num);
}
}
public class sourceInterrupt {
public static void main(String[] AUG){
thread T = new thread();
Thread t_1 = new Thread(new threadTest(T,1));
Thread t_2 = new Thread(new threadTest(T,2));
Thread t_3 = new Thread(new threadTest(T,3));
t_1.start();
t_2.start();
t_3.start();
}
}
由此可见,运行多线程时,3个线程都是同时间一起运行的,并且每次都是不一样的
- 已锁定
class thread{
synchronized void play(int n){// Locking this method.
System.out.println("Running Thread No: "+n);
try{
Thread.sleep(3);
}catch(InterruptedException e){
System.out.println("Thread No: "+ n + " Error!");
}
System.out.println("# Ending Thread No: "+n);
}
}
class threadTest implements Runnable{
thread obj;
int num;
threadTest(thread o, int n){
this.obj = o;
this.num = n;
}
public void run(){
this.obj.play(this.num);
}
}
public class sourceInterrupt {
public static void main(String[] AUG){
thread T = new thread();
Thread t_1 = new Thread(new threadTest(T,1));
Thread t_2 = new Thread(new threadTest(T,2));
Thread t_3 = new Thread(new threadTest(T,3));
t_1.start();
t_2.start();
t_3.start();
}
}
加入锁定后,每个线程都会在一个时间内单独的运行,并且下一个线程只能等上一个结束才能进入运行。
叨叨几句... NOTHING