一、创建多线程的方式1--继承Thread类
步骤:1自定义类MyThread继承Thread类 2MyThread里面重写run()方法 3创建对象 4启动线程
1 package test; 2 3 public class MyThread extends Thread{ 4 @Override 5 public void run() { 6 // TODO Auto-generated method stub 7 //super.run(); 8 for(int i=0;i<100;i++){ 9 System.out.PRintln(getName()+": "+i );10 }11 }12 }
1 package test; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 MyThread mt=new MyThread(); 6 mt.start(); 7 MyThread mt2=new MyThread(); 8 mt2.start(); 9 10 }11 }
二、线程基础
1获得线程名称
1 getName()
2设置线程名称
my1.setName("线程1");
3返回当前正在执行的线程的名称
Thread.currentThread().getName();
4线程优先级
1-10,默认是5
获得优先级
my1.getPriority()
设置优先级
my1.setPriority(2);
三、线程控制
1线程睡眠
1 package test; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 MyThread mt=new MyThread(); 6 MyThread mt2=new MyThread(); 7 MyThread mt3=new MyThread(); 8 9 mt.setName("进程1");10 mt2.setName("进程2");11 mt3.setName("进程3");12 13 mt.start();14 mt2.start();15 mt3.start();16 }17 }
1 package test; 2 import java.util.Date; 3 public class MyThread extends Thread{ 4 @Override 5 public void run() { 6 // TODO Auto-generated method stub 7 //super.run(); 8 for(int i=0;i<100;i++){ 9 System.out.println(getName()+": "+i +" " +new Date());10 //睡眠11 try {12 Thread.sleep(1000);13 } catch (InterruptedException e) {14 // TODO Auto-generated catch block15 e.printStackTrace();16 }17 }18 }19 }
2线程加入
1 package test; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 MyThread mt=new MyThread(); 6 MyThread mt2=new MyThread(); 7 MyThread mt3=new MyThread(); 8 9 mt.setName("进程1");10 mt2.setName("进程2");11 mt3.setName("进程3");12 13 mt.start();14 //线程加入 该线程执行完毕,其他线程才能执行15 try {16 mt.join();17 } catch (InterruptedException e) {18 // TODO Auto-generated catch block19 e.printStackTrace();20 }21 mt2.start();22 mt3.start();23 }24 }
1 package test; 2 import java.util.Date; 3 public class MyThread extends Thread{ 4 @Override 5 public void run() { 6 // TODO Auto-generated method stub 7 //super.run(); 8 for(int i=0;i<100;i++){ 9 System.out.println(getName()+": "+i +" " +new Date());10 //睡眠11 try {12 Thread.sleep(100);13 } catch (InterruptedException e) {14 // TODO Auto-generated catch block15 e.printStackTrace();16 }17 }18 }19 }
3线程礼让
1 package test; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 MyThread mt=new MyThread(); 6 MyThread mt2=new MyThread(); 7 MyThread mt3=new MyThread(); 8 9 mt.setName("进程1");10 mt2.setName("进程2");11 12 mt.start(); 13 mt2.start();14 15 }16 }
1 package test; 2 import java.util.Date; 3 public class MyThread extends Thread{ 4 @Override 5 public void run() { 6 // TODO Auto-generated method stub 7 //super.run(); 8 for(int i=0;i<100;i++){ 9 System.out.println(getName()+": "+i +" " +new Date());10 //线程礼让11 Thread.yield();12 }13 }14 }
4守护线程 主线程挂了,守护线程都会挂掉
1 package test; 2 3 import java.util.Date; 4 5 public class MyThread extends Thread { 6 @Override 7 public void run() { 8 // TODO Auto-generated method stub 9 // super.run();10 for (int i = 0; i < 20; i++) {11 System.out.println(getName() + ": " + i + " " + new Date());12 13 }14 }15 }
1 package test; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 MyThread mt=new MyThread(); 6 MyThread mt2=new MyThread(); 7 MyThread mt3=new MyThread(); 8 9 mt.setName("进程1");10 mt2.setName("进程2");11 12 //设置守护线程13 mt.setDaemon(true);14 mt2.setDaemon(true);15 16 mt.start(); 17 mt2.start();18 19 //改主线程的名字为“刘备”20 Thread.currentThread().setName("刘备");21 for(int x=0;x<5;x++){22 System.out.println(Thread.currentThread().getName()+": "+x);23 }24 25 }26 }
5进程中断
1 package test; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 MyThread mt = new MyThread(); 6 7 mt.start(); 8 //进程中断 9 //超过3秒不醒,则中断10 try {11 Thread.sleep(3000);12 mt.interrupt();13 } catch (InterruptedException e) {14 // TODO Auto-generated catch block15 e.printStackTrace();16 }17 18 }19 }
1 package test; 2 3 import java.util.Date; 4 5 public class MyThread extends Thread { 6 @Override 7 public void run() { 8 System.out.println("开始执行:"+new Date()); 9 try {10 Thread.sleep(10000);11 } catch (InterruptedException e) {12 // TODO Auto-generated catch block13 //e.printStackTrace();14 System.out.println("线程被终止了");15 }16 System.out.println("结束执行:"+new Date());17 }18 }
新闻热点
疑难解答