1、线程(Thread)
- 进程(Process):操作系统中,“同时”运行的多个程序
- 线程(Thread):一个应用程序中,“同时”运行的多个任务,例如:一边下载,一边播放;等等
- 例如:QQ可以同时支持聊天,还能够“同时”下载文件
- youku播放视频,“同时”还能下载视频,“同时”弹幕出现
“同时”是怎么实现的?实际上是“时间片分轮转分时”
2、如何实现多线程?
- 案例:下载文件,播放文件,如果不用多线程,将会串行运行
-实现多线程两种方法 - 1、让线程所在的类继承java.lang.Thread;
- 将线程实现代码写在从Thread中重写的run函数中;
- 使用线程类的start函数,启动线程,调用run
例子:同时下载文件和播放音乐
class Player extends Thread{
public void run()
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("音乐播放"+i*10+"%");
}
}
}
class DownLoader extends Thread{//②
public void run ()//①
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("文件下载"+i*10+"%");
}
}
}
class Test{
public static void main (String[] args) {
Player p = new Player();
DownLoader d = new DownLoader();
p.start();//③
d.start();
}
}
class Player extends Thread{
public void run() {
this.play();
}
public void play()
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("音乐播放"+i*10+"%");
}
}
}
class DownLoader extends Thread{//②
public void run() {
this.play();
}
public void play ()//①
{
for(int i = 0;i <= 10;i++)
{
try {
Thread.sleep(1000);
}catch(Exception e) {}
System.out.println("文件下载"+i*10+"%");
}
}
}
class Test{
public static void main (String[] args) {
Player p = new Player();
DownLoader d = new DownLoader();
p.start();//③
d.start();
}
}
- 2、让线程所在的类实现java.lang.Runnable接口;
- 将线程实现代码写在从Runnable中重写的run函数中;
- 实例化该类对象,将对象传入新的线程对象,使用其start函数,调用run
class Downloader implements Runnable{//①
public void run(){ //②
for(int i=1;i<=10;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("下载进度:" + i*10 + "%");
}
}
}
class Player implements Runnable{
public void run(){
for(int i=1;i<=10;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("播放进度:" + i*10 + "%");
}
}
}
class Test{
public static void main (String[] args) {
Downloader d = new Downloader();
Player p = new Player();
Thread t1 = new Thread(d); //③
Thread t2 = new Thread(p);
t1.start();
t2.start();
}
}
- 有何区别?如何选择?
- 方法1,让代码直接成为线程对象;方法2,让代码可以包装进线程对象;方法1相当于线程独立运行,方法2让代码依附线程运行;
- 方法2可以让一段代码依附多个线程,实现“一段代码的多线程化”;实际上使用时,方法2因为是实现了Runnable接口,因此同时类还可以继承其他类;
class AAA extends XXX implements Runnable{}
3、线程的控制
- 包括线程开始、暂停、继续、终止
- 线程开始:start函数
- 线程暂停:suspend函数
- 线程继续:resume
- 终止: stop
例子:播放音乐,3秒之后,暂停5秒,再次运行
class Downloader extends Thread{//①
public void run(){ //②
for(int i=1;i<=10;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("下载进度:" + i*10 + "%");
}
}
}
class Test{
public static void main (String[] args) throws Exception{
Downloader d = new Downloader();
d.start();
Thread.sleep(3000);
d.suspend();
Thread.sleep(4000);
d.resume();
Thread.sleep(2000);
d.stop();
}
}
----以上方法是不安全的,要避免使用!因为以上方法有deadlock-prone(可能造成死锁)
- suspend函数在线程暂停时,仍然持有线程占用的资源,其他线程不能访问该资源,只能等待resume之后运行完毕释放后才能访问。如果循环等待,则造成死锁
- 如何解决?不要使用上面几个函数
- 线程暂停,就是让线程结束;线程重新开始,就是新开启线程;线程结束的标志:run函数运行完毕
-注意保存线程暂停之后的运行状态(保存现场)当前状态
- 线程暂停,就是让线程结束;线程重新开始,就是新开启线程;线程结束的标志:run函数运行完毕
package customer;
class Downloader extends Thread{//①
boolean RUN = true;
static int percent = 1;
public void run(){ //②
for(int i=percent;i<=10 && RUN;i++){
try{ Thread.sleep(1000);} catch(Exception e){}
System.out.println("下载进度:" + i*10 + "%");
percent = i+1;
}
}
}
class Test{
public static void main (String[] args) throws Exception{
Downloader d = new Downloader();
d.start();
Thread.sleep(3000);
d.RUN = false;
Thread.sleep(4000);
d = new Downloader();
d.start();
Thread.sleep(2000);
d.RUN = false;
}
}
4、线程同步
多个线程访问同一资源。
两个线程进行文件下载。
class Downloader extends Thread{//①
boolean RUN = true;
int i = 0;
public void run(){ //②
while(RUN){
try{ Thread.sleep(1000);} catch(Exception e){}//这个很重要,不能在sy里面
synchronized(this){//这段代码运行时CPU不会被抢占
i++;
System.out.println("文件下载:" + i*10 + "%");
if(i>=9) break;
}
}
}
}
class Test{
public static void main (String[] args) throws Exception{
Downloader d = new Downloader();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
t2.start();
}
}
synchronized:同步代码,保持原子性(代码运行中,别的线程不能抢占)