总结一下学习的java多线程!!!
实现线程的方法有两种!第一种是继承Thread类,然后重写里面的run方法,第二种是继承接口Runnablle,重写run方法!
首先第一种:
1 public class ThreadDemo extends Thread{ 2 public void run(){ 3 for(int i=0;i!=10;++i) 4 System.out.println("demo"+i); 5 } 6 } 7 public class ThreadTest { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 ThreadDemo d=new ThreadDemo(); 15 d.start(); 16 for(int i=0;i<10;++i) 17 System.out.println("main"+i); 18 } 19 20 }
然后是第二种:
1 public class ThreadDemo implements Runnable{ 2 public void run(){ 3 for(int i=0;i!=10;++i) 4 System.out.println("demo"+i); 5 } 6 } 7 public class ThreadTest { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Thread d=new Thread(new ThreadDemo()); 15 d.start(); 16 for(int i=0;i<10;++i) 17 System.out.println("main"+i); 18 } 19 20 }
两种方法在实现上是相通的,但是细节是不同的!首先第一种方法不能够继承与其他类!因为java是单继承的!同时它也不能共享变量!
所以优先选择第二种方法!
然后是线程的同步,多线程的程序一个关键点就是线程的同步,多个线程对一个对象进行修改,势必会造成错乱,所以用同步的方法为对象加上一把锁!
线程同步也有方法的同步,也有同步语句!
先来看看没有用同步方法的代码:
1 public class ThreadDemo implements Runnable{ 2 int count=5; 3 @Override 4 public void run() { 5 // TODO Auto-generated method stub 6 while(count>0){ 7 System.out.println(Thread.currentThread().getName()+"----->"+count); 8 --count; 9 } 10 } 11 } 12 public class ThreadText { 13 14 /** 15 * @param args 16 */ 17 public static void main(String[] args) { 18 ThreadDemo t=new ThreadDemo(); 19 Thread d1=new Thread(t); 20 Thread d2=new Thread(t); 21 Thread d3=new Thread(t); 22 d1.start(); 23 d2.start(); 24 d3.start(); 25 } 26 27 }
得到的结果
Thread-0----->5
Thread-2----->5
Thread-1----->5
Thread-2----->3
Thread-0----->4
Thread-2----->1
Thread-1----->2
对方法的同步:
1 public class ThreadDemo implements Runnable{ 2 int count=5; 3 @Override 4 public synchronized void run() { 5 // TODO Auto-generated method stub 6 while(count>0){ 7 System.out.println(Thread.currentThread().getName()+"----->"+count); 8 --count; 9 } 10 } 11 } 12 13 public class ThreadText { 14 15 /** 16 * @param args 17 */ 18 public static void main(String[] args) { 19 ThreadDemo t=new ThreadDemo(); 20 Thread d1=new Thread(t); 21 Thread d2=new Thread(t); 22 Thread d3=new Thread(t); 23 d1.start(); 24 d2.start(); 25 d3.start(); 26 } 27 28 }
得到的结果
Thread-0----->5
Thread-0----->4
Thread-0----->3
Thread-0----->2
Thread-0----->1
然后是同步语句的使用
1 public class ThreadDemo implements Runnable{ 2 int count=5; 3 @Override 4 public void run() { 5 // TODO Auto-generated method stub 6 synchronized(this){ 7 while(count>0){ 8 System.out.println(Thread.currentThread().getName()+"----->"+count); 9 --count; 10 } 11 } 12 } 13 } 14 public class ThreadText { 15 16 /** 17 * @param args 18 */ 19 public static void main(String[] args) { 20 ThreadDemo t=new ThreadDemo(); 21 Thread d1=new Thread(t); 22 Thread d2=new Thread(t); 23 Thread d3=new Thread(t); 24 d1.start(); 25 d2.start(); 26 d3.start(); 27 } 28 29 }
得到的结果:
Thread-0----->5
Thread-0----->4
Thread-0----->3
Thread-0----->2
Thread-0----->1
只有第一个线程运行!!!!其他的线程阻塞了!
线程的等待和通知:
wait notify notifyAll
wait() 方法的用法
while(condition){ //判断条件是否成立不成立就执行wait()方法 阻塞自己等待条件成立
wait();
}
wait()方法最好是用在while循环中而不是用if语句去判断条件,因为可能在条件成立后其他线程又会修改条件!
notify() notifyAll()
if(condition){
notify();
}
三种方法必须在同步方法或者是同步语句中使用,不然会抛出异常!
例子:
打印时间线程每过一秒打印一个数字,打印消息线程每过十五秒打印消息!
1 public class Printmessage implements Runnable{ 2 WaitDemo wd; 3 public Printmessage(WaitDemo w){ 4 this.wd=w; 5 } 6 @Override 7 public void run() { 8 // TODO Auto-generated method stub 9 while(true){ 10 wd.PrintMessage(); 11 try { 12 Thread.sleep(1000); 13 } catch (InterruptedException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 } 18 } 19 20 } 21 22 public class WaitDemo implements Runnable{ 23 boolean flag=false; 24 /** 25 * @param args 26 */ 27 public static void main(String[] args) { 28 // TODO Auto-generated method stub 29 WaitDemo demo=new WaitDemo(); 30 Thread t1=new Thread(demo); 31 Thread t2=new Thread(new Printmessage(demo)); 32 t1.start(); 33 t2.start(); 34 } 35 36 @Override 37 public void run() { 38 // TODO Auto-generated method stub 39 PrintTime(); 40 } 41 public void PrintTime(){ 42 for(int i=1;;++i){ 43 if(i%15==0){ 44 flag=true; 45 notifyother(); 46 try { 47 Thread.sleep(1000); 48 } catch (InterruptedException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } 52 flag=false; 53 } 54 System.out.println(i); 55 try { 56 Thread.sleep(1000); 57 } catch (InterruptedException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } 61 } 62 } 63 public void PrintMessage(){ 64 synchronized(this){ 65 while(!flag) 66 try { 67 wait(); 68 } catch (InterruptedException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 System.out.println("the message!"); 73 } 74 } 75 public synchronized void notifyother(){ 76 notifyAll(); 77 } 78 79 }
多线程的基本知识还有很多 再看