5.JAVA语言基础部分—多线程

一个应用有一个进程,一个进程里可以用多个线程

1)定义

定义线程有两种方式,一是继承java.lang.Thread类,二是实现java.lang.Runnable接口。其实Thread类就是实现了Runnable接口的。

//方法一:继承Thread,并重写run()
public class OrdersThread extends Thread {
	//该类实例化时并不会立即执行run(),而是在实例上调用start()才会执行
	@Override
	public void run() {
		//需要线程执行的内容放在run方法
		for (int i = 0; i < 10; i++) {
			System.out.println(i);
		}
	}
}
 
//调用 
	public static void main(String[] args) {
		OrdersThread ot=new OrdersThread();
		//调用start()才真正开始线程
		ot.start();
	}

方法二:

//方法二实现接口Runnable
public class MyRunnable implements Runnable{

	@Override
	public void run() {
		// 这里放需要执行的代码
	}
}

// 调用
	public static void main(String[] args) { 
		MyRunnable ta = new MyRunnable();
		//通过实现Runnable接口方式,需要使用Thread实现来启动
		Thread thread1 = new Thread(ta);
		thread1.start();
	}

方法三:Runnable方式还可以通过匿名类的写法实现

public static void main(String[] args) { 
		//匿名类实现的方式,匿名类实现接口Runnable
		Thread thread2=new Thread(new Runnable() {
			@Override
			public void run() {
				//需要执行的代码
			}
		});
	}

 

2)线程生命周期

image

Thread.sleep(毫秒值):进入休眠,单位毫秒。线程不会释放对象锁,不让出系统资源,占用着。直接使用Thread类调用sleep,不用实例。谁调用谁睡觉去,即使在当前线程调用别的线程的sleep,睡觉的不是当前线程,其他线程不受影响。sleep的唤醒要等时间到了自动醒过来,如果时间不到只能调用它的实例的interrupt()强行中断。

wait():也是等待,一般不需要指定时间,需要使用notify()/notifyAll()来唤醒。它跟sleep不同,wait是释放对象锁的,不占资源,一般是用到同步模块(synchronized)中去。如下面所示,也有多线程同步的写法;

public class MyRunnable implements Runnable {
  private static String flag="";
	@Override
	public void run() {
		//多线程同步
		//这里可以定义一个变量,或者使用当前类this,或空格“” 都可以
		synchronized (flag) {
			//这里notify()是唤醒当前MyRunnable的上一个实例继承执行(上次实例中wait()后面的代码继承执行)
			//这里notifyAll()是唤醒当前MyRunnable的【所有所有所有】实例中有wait()的实例继承执行
			//只能在当前的run调用notity,其它地方行
			flag.notify();
			
			for (int i = 1000; i < 1100; i++) {
				if (i == 1050) {
					try {
						System.out.println("MyRunnable暂停了");
						flag.wait();
					} catch (InterruptedException e) { 
						e.printStackTrace();
					}
				} 
				System.out.println(i);
			}
		}
	}
}

3)线程加入

有两个线程A、B,在执行A时某一步骤时要先让B执行完后再继续执行A,可以使用join()

package com.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.lang.reflect.*;

public class Main {
	//定义未赋值的变量放在外面
	 private static Thread a;
	 private static Thread b;
	public static void main(String[] args) { 	
		a = new Thread(new Runnable() {
			public void run() {
				for (int i = 10; i < 20; i++) {
					try {
						Thread.sleep(100);
						print(i);
						if (i == 12) {
							//到此处时,只有b完成后才会继续往下走
							b.join();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		a.start();
	 
		b = new Thread(new Runnable() {
			public void run() {
				for (int i = 30; i < 40; i++) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) { 
						e.printStackTrace();
					}
					print(i);
				}
			}
		});
		b.start();
	}
}

4)线程中断

方法一:在线程继承类或实现Runnable接口的类里定义一个变量,然后在run()处判断该变量值为多少时就使用break;退出。该方法有限制,当线程里使用了sleep()或wait()后就无法退出了,此时应使用方法interrup()退出。

方法二:强行中断线程使用interrup(),调用线程的实例的interrup()方法,线程执行处就会抛出异常InterruptedException,使用try catch捕获使用break退出即可。

public class OrdersThread extends Thread { 
	@Override
	public void run() { 
		for (int i = 0; i < 10; i++) {
			 if(i==5)
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) { 
					//只要在run()里使用try catch捕获InterruptedException异常,然后使用break处理并退出了
					System.out.println("强行退出了"); 
					break;
				}
			System.out.println(i);
		}
	}
}

 

OrdersThread t3=new OrdersThread();
		t3.start();
		t3.interrupt();//实例里强行退出

5)线程优先线

线程优先级有常数:1-10,默认为5

使用setPriority(1~10的整数)

OrdersThread t3=new OrdersThread();
		//注意值的范围是1-8
		t3.setPriority(8);
		//也可以使用预先定义的枚举指定
		//t3.setPriority(Thread.NORM_PRIORITY);
		t3.setName("线程名称");//这里设置线程名称的写法
		//最后才能start
		t3.start(); 
		

6)线程同步

public class MyRunnable implements Runnable {
	@Override
	public void run() {
		// Object可以是任何,一般是定义一个静态变量,或者当前类实例
		synchronized (Object) {
			// 需要步的代码
			for (int i = 1000; i < 1100; i++) {
				System.out.println(i);
			}
		}

	}
}

线程同步方法

//同步方法使用synchronized修饰
	public  synchronized String getName() {
		return "";
	}
posted @ 2018-10-21 13:05  evemen  阅读(163)  评论(0编辑  收藏  举报