第一节 多线程的引入
定义:同时对多项任务加以控制
public class Music extends Thread{
public void run(){
for(int i=0;i<10;i++){
try {
Thread.sleep(100);
System.out.println("听音乐");
} catch (InterruptedException ex) {
Logger.getLogger(Music.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
》》》》》》》》
public class Eat extends Thread{
public void run(){
for(int i=0;i<10;i++){
try {
Thread.sleep(100);
System.out.println("吃饭");
} catch (InterruptedException ex) {
Logger.getLogger(Eat.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
》》》》》》》》》
public class Demo1 {
/**
* @param args the command line arguments
*/
public static void music(){
for(int i=0;i<10;i++){
System.out.println("听音乐");
}
}
public static void eat(){
for(int i=0;i<10;i++){
System.out.println("吃饭");
}
}
public static void main(String[] args) {
/* music();
eat(); */
Music musicThread=new Music();
Eat eatThread=new Eat();
musicThread.start();
eatThread.start();
}
}
第二节 java多线程实现
1,继承Thread类
public class Thread1 extends Thread{
private int baozi=1;
private String threadName;
public Thread1(String threadName){
super();
this.threadName=threadName;
}
public void run(){
while(baozi<=10){
try {
Thread.sleep(100);
System.out.println(threadName+"吃第"+baozi+"个包子");
baozi++;
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[]){
Thread1 thread1=new Thread1("张三");
Thread1 thread2=new Thread1("李四");
thread1.start();
thread2.start();
}
}
2,实现Runnable接口
public class Thread2 implements Runnable{
private int baozi=1;
private String threadName;
public Thread2(String threadName){
super();
this.threadName=threadName;
}
public synchronized void run(){
while(baozi<=10){
try {
Thread.sleep(100);
System.out.println(threadName+"吃第"+baozi+"个包子");
baozi++;
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[]){
/*Thread2 t1=new Thread2("张三");
Thread2 t2=new Thread2("李四");
Thread t11=new Thread(t1);
Thread t22=new Thread(t2);
t11.start();
t22.start();*/
Thread2 t1=new Thread2("超级张三");
Thread t11=new Thread(t1);
Thread t12=new Thread(t1);
Thread t13=new Thread(t1);
t11.start();
t12.start();
t13.start();
}
}
第三节 线程状态
进程状态转换图
1,创建状态
在程序中用构造方法创建一个线程对象后,新的线程对象便处于新建状态,此时,它已经有了相应的内存空间和其他资源,但还处于不可运行状态。新建一个线程对象可采用Thread类的构造方法来实现,例如“Thread thread=new Thread();”。
2,就绪状态
新建线程对象后,调用的该线程的start()方法就可以启动线程。当线程启动时,线程进入就绪状态。此时,线程将进入线程队列排队,等待CPU服务,这表明它已具备运行条件。
3,运行状态
当就绪状态的线程被调用并获得处理器资源时,线程就进入了运行状态。此时,自动调用该线程对象的run()方法。Run()方法定义了该线程的操作和功能。
4,堵塞状态
一个正在执行的线程在某些特殊情况下,如被人为挂起或需要执行耗时的输入/输出操时,将让出CPU并暂时中止自己的执行,进入堵塞状态。堵塞时,线程不能进入排队队列,只有当被引起堵塞的原因被消除后,线程才可以转入就绪状态。
5,死亡状态
线程调用stop()方法或run()方法执行结束后,即处于死亡状态。处于死亡状态的线程不具有继续运行的能力
第四节 线程常用方法
1,getName() 返回该线程的名称
2,currentThread() 返回对当前正在执行的线程对象的引用
3,isAlive() 测试线程是否处于活跃状态
4,sleep() 线程休眠
5,setPriority(int newPriority) 更改线程的优先级
6,yield() 暂停当前正在执行的线程对象,并执行其他线程
public class Demo implements Runnable{public void run(){
for(int i=0;i<=9;i++){
try {
//获取当前对象
Thread t=Thread.currentThread();
Thread.sleep(10);
System.out.println(t.getName()+":"+i); //返回线程的名称
} catch (InterruptedException ex) {
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo demo=new Demo();
new Thread(demo).start();
new Thread(demo).start();
new Thread(demo,"线程3").start();
}
}
》》》》》》》》》》》
public class Demo2 implements Runnable{
public void run(){
for(int i=0;i<=9;i++){
try {
//获取当前对象
Thread t=Thread.currentThread();
Thread.sleep(10);
System.out.println(t.getName()+":"+i); //返回线程的名称
} catch (InterruptedException ex) {
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
Demo2 demo=new Demo2();
Thread t1=new Thread(demo);
System.out.println( "t1是否活动:"+t1.isAlive());
t1.start();
System.out.println( "t1是否活动:"+t1.isAlive());
}
}
》》》》》》》》》》》》》》
public class Demo3 implements Runnable{
public void run(){
for(int i=0;i<=9;i++){
try {
//获取当前对象
Thread t=Thread.currentThread();
Thread.sleep(1000);
System.out.println(t.getName()+":"+i); //返回线程的名称
} catch (InterruptedException ex) {
Logger.getLogger(Demo3.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo3 demo=new Demo3();
new Thread(demo).start();
new Thread(demo).start();
new Thread(demo,"线程3").start();
}
}
》》》》》》》》》》》》》》
public class Demo4 implements Runnable{
public void run(){
for(int i=0;i<=9;i++){
try {
//获取当前对象
Thread t=Thread.currentThread();
Thread.sleep(10);
System.out.println(t.getName()+":"+i); //返回线程的名称
} catch (InterruptedException ex) {
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo4 demo=new Demo4();
Thread t1=new Thread(demo,"线程A");
Thread t2=new Thread(demo,"线程B");
Thread t3=new Thread(demo,"线程C");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
》》》》》》》》》》》》》》
public class Demo5 implements Runnable{
public void run(){
for(int i=0;i<=9;i++){
try {
//获取当前对象
Thread t=Thread.currentThread();
Thread.sleep(10);
System.out.println(t.getName()+":"+i); //返回线程的名称
if(i==5){
System.out.println("线程礼让:");
Thread.currentThread().yield();
}
} catch (InterruptedException ex) {
Logger.getLogger(Demo5.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo5 demo=new Demo5();
new Thread(demo,"线程A").start();
new Thread(demo,"线程B").start();
}
}
第五节 线程同步
1,同步方法
2,同步锁
public class Demo implements Runnable{private int baozi=10;
/**
* @param args the command line arguments
*/
//同步方法
public synchronized void run(){
while(baozi>0){
System.out.println(Thread.currentThread().getName()+"吃了第"+baozi+"个包子");
baozi--;
}
}
public static void main(String[] args) {
Demo demo=new Demo();
new Thread(demo,"张三").start();
new Thread(demo,"李四").start();
new Thread(demo,"王五").start();
}
}
》》》》》》》》》》》》
public class Demo2 implements Runnable{
private int baozi=10;
/**
* @param args the command line arguments
*/
public void run(){
synchronized(this){
//同步块
while(baozi>0){
System.out.println(Thread.currentThread().getName()+"吃了第"+baozi+"个包子");
baozi--;
}
}
}
public static void main(String[] args) {
Demo2 demo=new Demo2();
new Thread(demo,"张三").start();
new Thread(demo,"李四").start();
new Thread(demo,"王五").start();
}
}