一:线程和进程
- 线程:程序中单独顺序的控制流
线程本身依靠程序中运行
线程是程序中顺序控制流,只能使用分配给程序的资源和环境 - 进程:执行中的程序
一个进程可以包含一个或多个线程
一个进程至少包含一个线程 - 单线程:
程序中只存在一个线程,实际上主方法就是一个主线程 - 多线程:
多线程是在一个程序中运行多个任务
多线程的目的是更好的使用CPU资源
- 在Java中,线程的实现有两种
继承Thread类
实现Runable接口 - Thread类:
Thread类是在java.lang包中定义的,继承Thread类必须重写run方法
定义格式:
class className extends Thread{
run(){};
}
public static void main(String[] args) { MyThread t1 = new MyThread("A"); MyThread t2 = new MyThread("B"); t1.start(); t2.start(); }
线程的启动是通过start()方法,并不是通过run方法的。public class MyThread extends Thread{ String name; public MyThread(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(name + ":" +i); } super.run(); } }
- runable:
public static void main(String[] args) { /* MyThread t1 = new MyThread("A"); MyThread t2 = new MyThread("B");*/ MyRunable a1 = new MyRunable("A"); MyRunable a2 = new MyRunable("B"); /*t1.start(); t2.start();*/ Thread t1 = new Thread(a1); Thread t2 = new Thread(a2); t1.start(); t2.start(); }
public class MyRunable implements Runnable{
private String name;
public MyRunable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(name + ":" + i);
}
}
三:线程的状态
创建状态:准备好了一个多线程的对象
就绪状态:调用了start()方法,等待CPU进行调度 运行状态:执行run()方法 阻塞状态:暂时停止执行,可能将资源交给其他线程使用
终止状态:线程销毁
四:线程的常用方法
- 取得线程名称
getName() - 取得线程当前对象
cunrrentThread() - 判断线程是否启动
isAlive() - 线程的强行运行
join() - 线程的休眠
sleep() - 线程的礼让
yield()
class RunnableDemo implements Runnable{
private String name;
public RunnableDemo(String name) {
this.name = name;
}
@Override
public void run() {
// System.out.println("当前线程对象:" + Thread.currentThread().getName());
for (int i = 0; i < 50; i++) {
System.out.println(name + ":" + i);
if (i == 10) {
System.out.println("礼让");
Thread.yield();
}
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
}
public class ThreadDemo03 {
public static void main(String[] args) {
RunnableDemo r1 = new RunnableDemo("A");
RunnableDemo r2 = new RunnableDemo("B");
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
// System.out.println(t1.isAlive());
t1.start();
// System.out.println(t1.isAlive());
t2.start();
}
}
五:多线程的优先级
- 优先级顺序设置
1-MIN_PRIORITY
10-MAX_PRIORITY
5-NORM_PRIORITY
如果什么都不设置默认值为5
class ThRun implements Runnable{ private String name; public ThRun(String name) { this.name = name; } @Override public void run() { for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); System.out.println(name + ":" + i); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class ThreadDemo04 { public static void main(String[] args) { Thread t1 = new Thread(new ThRun("A")); Thread t2 = new Thread(new ThRun("B")); Thread t3 = new Thread(new ThRun("C")); t1.setPriority(Thread.MAX_PRIORITY); t2.setPriority(Thread.NORM_PRIORITY); t3.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); t3.start(); } }
六:线程同步(同步与死锁)
- 同步代码块
在代码块上加上“synchronized”关键字,则此代码块就称为同步代码块 - 同步代码块格式
synchronized(同步对象){
需要同步的代码块;
} - 同步方法
除了代码块可以同步,方法也是可以同步的 - 方法同步格式
synchronized void 方法名称(){}
class MyThreadDemo implements Runnable{
private int ticket = 5;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
/*synchronized (this) {
if (ticket > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("车票:" + ticket--);
}
}*/
tell();
}
}
public synchronized void tell() {
if (ticket > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("车票:" + ticket--);
}
}
}
public class ThreadDemo05 {
public static void main(String[] args) {
MyThreadDemo m = new MyThreadDemo();
Thread t1 = new Thread(m);
Thread t2 = new Thread(m);
Thread t3 = new Thread(m);
t1.start();
t2.start();
t3.start();
}
}
七:线程生命周期