1 /** 2 * 线程 3 * 线程有就绪,阻塞,运行三种状态 4 * a.线程是轻量级的进程 5 * b.线程没有独立的地址空间(内存空间) 6 * c.线程是由进程创建的(寄生在进程) 7 * d.一个进程可以拥有多个线程,这就是我们常说的多线程编程 8 * f.线程的多种状态 9 * 新建状态(new)--就绪状态(Runnable)--运行状态(Running)--阻塞状态(Blocked)--死亡状态(Dead) 10 * 实现类的两种方法 11 * a.继承Thread类,并重写run函数 12 * b.实现Runnable接口,并重写run函数 13 */ 14 package com.test4; 15 16 public class day23 { 17 public static void main(String []args) { 18 //创建一个Cat对象 19 Catt catt=new Catt(); 20 //启动线程 会启动run函数的运行 21 catt.start(); 22 } 23 } 24 25 class Catt extends Thread{ //继承Thread类 26 int time=0; 27 //重写run函数 28 public void run() { 29 while (true) { 30 //休眠一秒 sleep会让线程进入Bllocked状态,并释放资源 31 try { 32 Thread.sleep(1000); 33 } catch (InterruptedException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 time++; 38 System.out.println(time); 39 if (time==10) { 40 break; 41 } 42 System.out.println("哈喽啊树哥"); 43 } 44 45 } 46 47 }
1 //用接口的方式实现单线程 2 package com.test4; 3 4 import java.io.IOException; 5 import java.nio.CharBuffer; 6 7 public class day24 { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 //创建一个对象 12 Bread bread=new Bread(); 13 //创建一个Thread对象 并把创建的bread加入到Thread里面 14 Thread d=new Thread(bread); 15 //启动线程 16 d.start(); 17 } 18 } 19 class Bread implements Runnable{ 20 21 int time=0; 22 //重写run函数 23 public void run() { 24 while (true) { 25 try { 26 Thread.sleep(1000); 27 } catch (Exception e) { 28 // TODO: handle exception 29 e.printStackTrace(); 30 } 31 time++; 32 System.out.println(time+"哈喽啊树哥"); 33 if (time==10) { 34 break; 35 } 36 } 37 } 38 39 }
1 /** 2 * 两个线程同时跑的案例 3 */ 4 package com.test4; 5 6 import java.sql.Time; 7 8 9 10 public class day25 { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Pig pig=new Pig(5); 15 Pan pan=new Pan(5); 16 Thread t1=new Thread(pig); 17 Thread t2=new Thread(pan); 18 t1.start(); 19 t2.start(); 20 21 22 } 23 24 } 25 //打印 26 class Pig implements Runnable{ 27 int n=0; 28 int times=0; 29 public Pig(int n) { 30 this.n=n; 31 } 32 public void run() { 33 while (true) { 34 try { 35 Thread.sleep(1000); 36 } catch (Exception e) { 37 // TODO: handle exception 38 e.printStackTrace(); 39 } 40 times++; 41 System.out.println("我是一个线程在输出第"+times+"个哈喽啊树哥"); 42 if (times==n) { 43 break; 44 } 45 } 46 } 47 } 48 //做数学题 49 class Pan implements Runnable{ 50 int n=0; 51 int times=0; 52 int res=0; 53 public Pan(int n) { 54 this.n=n; 55 } 56 public void run(){ 57 while (true) { 58 try { 59 Thread.sleep(1000); 60 } catch (Exception e) { 61 // TODO: handle exception 62 e.printStackTrace(); 63 } 64 res+=(++times); 65 System.out.println("当前结果是:"+res); 66 if(times==n) { 67 System.out.println("最后结果是:"+res); 68 break; 69 } 70 } 71 } 72 }
线程对象锁
1 /** 2 * 线程并发所带来的安全问题 3 * 如何解决并发问题 4 * synchronized对象锁 5 */ 6 7 package com.test4; 8 9 public class day26 { 10 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 Monkey mo=new Monkey(); 14 //新建三个线程 15 Thread t1=new Thread(mo); 16 Thread t2=new Thread(mo); 17 Thread t3=new Thread(mo); 18 //启动线程 19 t1.start(); 20 t2.start(); 21 t3.start(); 22 23 } 24 25 } 26 class Monkey implements Runnable 27 { 28 //定义1000个香蕉 29 private int banana=100; 30 31 @Override 32 public void run() { 33 // TODO Auto-generated method stub 34 35 while (true) { 36 37 //同步代码块this是对象本身 38 synchronized (this) { 39 40 //判断猴子吃香蕉 41 if (banana>0) { 42 //Thread.currentThread().getName()这个可以查看是哪个线程在启动,对调试很有用 43 System.out.println(Thread.currentThread().getName()+"在吃第"+banana+"个香蕉"); 44 try { 45 Thread.sleep(1000); 46 } catch (Exception e) { 47 // TODO: handle exception 48 e.printStackTrace(); 49 } 50 //吃掉一个香蕉就减掉一根 51 banana--; 52 53 }else { 54 break; 55 } 56 } 57 } 58 } 59 }
浙公网安备 33010602011771号