一.多线程
创建多线程方法一:
创建多线程继承Thread类+重写run(线程体)
package cn.inspur.myThread;
public class Rabbit extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("兔子跑了" + i + "步");
}
}
}
package cn.inspur.myThread;
public class Tortoise extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("\t乌龟跑了" + i + "步");
}
}
}
使用线程,创建子类对象+调用对象.start方法 线程的启动
package cn.inspur.myThread;
public class MyApp {
public static void main(String[] args) {
//创建子类对象
Rabbit rab=new Rabbit();
Tortoise tor=new Tortoise();
//调用start方法
rab.start();//不要调用run方法
tor.start();
}
}
创建线程方法二(使用Runnable,创建线程):
类实现Ruunnable接口+重写run()
package cn.inspur.myThread;
public class Web12306 implements Runnable {
private int num = 5;
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if (num <= 0)
break;
}
System.out.println(Thread.currentThread().getName()+"抢到了"+num--);
}
}
启动多线程 使用静态代理
1)创建真实角色
2)创建代理角色+真实角色引用
3)调用.start()启动线程
package cn.inspur.myThread;
public class Web12306App {
public static void main(String[] args) {
Web12306 web = new Web12306();
Thread t1 = new Thread(web,"01");
Thread t2 = new Thread(web,"02");
Thread t3 = new Thread(web,"03");
t1.start();
t2.start();
t3.start();
}
}
二. 线程状态
停止线程
1.自然终止:线程体使用的标识
2.外部干涉
package cn.inspur.myThread;
public class Demo01 {
public static void main(String[] args) {
Study study=new Study();
new Thread(study).start();
for (int i = 0; i < 100; i++) {
if (50==i) {//外部干涉
study.stop();
}
System.out.println("main..."+i);
}
}
}
class Study implements Runnable{
//1.线程类中定义线程体使用的标志
private boolean flag=true;
@Override
public void run() {
// TODO Auto-generated method stub
//2.线程体使用该标志
while(flag){
System.out.println("study.thread......");
}
}
//3.对外提供方法改变标识
public void stop() {
this.flag=false;
System.out.println("stoped");
}
}
阻塞线程
join:合并线程
package cn.inspur.myThread;
public class JoinDemo01 extends Thread {
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("join...." + i);
}
}
public static void main(String[] args) throws InterruptedException {
JoinDemo01 demo01 = new JoinDemo01();
Thread thread = new Thread(demo01);// 新生
thread.start();// 就绪
// CPU调度运行
for (int i = 0; i < 1000; i++) {
if (50 == i) {
thread.join();//main方法阻塞,去执行join,join完成后再执行main
System.out.print("**************");
}
System.out.println("\tmain...." + i);
}
}
}
Yield:暂停线程 静态方法(static)
package cn.inspur.myThread;
public class YieldDemo01 extends Thread {
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("Yield...." + i);
}
}
public static void main(String[] args) throws InterruptedException {
YieldDemo01 demo01 = new YieldDemo01();
Thread thread = new Thread(demo01);// 新生
thread.start();// 就绪
// CPU调度运行
for (int i = 0; i < 1000; i++) {
if (0 == i%20) {
Thread.yield();//main方法暂停,静态方法,下一个执行谁要看CPU
System.out.print("**************");
}
System.out.println("\tmain...." + i);
}
}
}
sleep:延迟线程
package cn.inspur.myThread;
import java.sql.Date;
import java.text.SimpleDateFormat;
public class SleepDemo01 {
public static void main(String[] args) throws InterruptedException {
Date endTime=new Date(System.currentTimeMillis()+10*1000);
long end=endTime.getTime();
while(true){
//输出
System.out.println(new SimpleDateFormat("mm:ss").format(endTime));
//等待一秒
Thread.sleep(1000);
//构建下一秒的时间
endTime=new Date(endTime.getTime()-1000);
//10秒内继续否则退出
if(end-10000>endTime.getTime()){
break;
}
}
}
}
三.优先级
优先级:是概率,不是绝对的优先级
static int MAX_PRIORITY 10
static int MIN_PRIORITY 1
static int NORM_PRIORITY 5
Thread t1=new Thread();
Thread t2=new Thread();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);