线程创建的两种方式:
(1)继承Thread类
(2)实现Runable接口
1、Thread类中常用方法包括start()、interrupt()、jion()、run()、等方法。其中start()为启动线程,run()为线程的主体方法,根据需要覆写即可。
2、Runable接口,实现Runable接口的类就可以成为线程,Runable接口只有一个run方法,实现Runable()接口后必须覆写run()方法。
//最简单的线程实现
package ThreadTest;
public class SimpleThread extends Thread {
public SimpleThread(String name) { //参数为线程名称
// TODO Auto-generated constructor stub
setName(name);
}
@Override
public void run() { //覆写run()方法
// TODO Auto-generated method stub
super.run();
int i = 0;
while (i++ < 5) {
try {
System.out.println(getName() + "执行步骤" + i);
Thread.sleep(1000); //休眠1秒
} catch (Exception e) {
// TODO: handle exception
}
}
}
public static void main(String[] args) {
SimpleThread thread1 = new SimpleThread("线程1");
SimpleThread thread2 = new SimpleThread("线程2");
thread1.start();
thread2.start();
}
}
//最简单的Runable()实现
package ThreadTest;
public class SimpleRunable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i++ < 12) {
try {
System.out.println(i);
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public static void main(String[] args) {
Thread thread1 = new Thread(new SimpleRunable(), "线程1");
thread1.start();
}
}
二、线程的控制
1、优先级 Thread.setPriority() 范围是1-10,默认是5; yield()礼让的暗示
2、挂起 sleep(); join()如果A正在运行,B.join()会使B线程先运行完,再回到A;
wait()、notify()、suspend()、resume() 挂起线程,直到收到notify或notifyAll()或resume()的消息为止;
3、状态:isAlive()检查线程是否存活 使用stop()强制死亡或者自然死亡。
三、线程同步
//同步锁synchronized的基础用法
package learn;
public class SyncThread extends Thread{
private char cha;
public SyncThread(char cha) {
this.cha = cha;
}
public void run() {
// TODO Auto-generated method stub
PrintClass.printch(cha);
System.out.println();
}
public static void main(String[] args) {
SyncThread t1 = new SyncThread('A');
SyncThread t2 = new SyncThread('B');
t1.start();
t2.start();
}
}
class PrintClass{
// public static synchronized void printch(char cha) {
public static void printch(char cha) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(cha);
}
}
}
//使用多线程实现进水出水
package Threadconnect;
public class ThreadA extends Thread{
Water water;
public ThreadA(Water waterArg) {
water = waterArg;
}
public void run() {
System.out.println("开始进水");
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println(i + "分钟");
} catch (Exception e) {
e.printStackTrace();
}
}
water.setWater(true); //设置水塘有水状态
System.out.println("进水完毕,水塘水漫");
synchronized (water) {
water.notify();
//notify()最多只能释放等待队列中的第一个线程,如果有多个线程在等,使用notifyAll()可释放所有线程 。
}
}
}
package Threadconnect;
public class ThreadB extends Thread{
Water water;
public ThreadB(Water waterArg) {
// TODO Auto-generated constructor stub
water = waterArg;
}
public void run() {
// TODO Auto-generated method stub
System.out.println("启动排水");
if (water.isEmpty()) {
synchronized (water) { //同步代码块synchronized
try {
System.out.println("水塘无水,排水等待中");
water.wait(); //使线程处于等待状态
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
System.out.println("开始排水。。。。。。");
for (int i = 5; i > 0; i--) {
try {
Thread.sleep(1000);
System.out.println(i + "分钟");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
water.setWater(false); //设置水塘无水状态
System.out.println("排水完毕");
}
}
package Threadconnect;
public class Water {
boolean water = false;
public boolean isEmpty() {
return water ? false : true;
}
public void setWater(boolean haveWater) {
this.water = haveWater;
}
public static void main(String[] args) {
Water water = new Water();
ThreadA threadA = new ThreadA(water);
ThreadB threadB = new ThreadB(water);
threadB.start();
threadA.start();
}
}