线程的四种创建模式以及使用实例

线程的创建主要有四种方式,

一种是直接继承Thread实现,

另一种是引用接口Runable。

 

也可以运用内部类实现接口的创建。但是用这种方法创建的线程只能用一次。以下就是线程的四种创建方式的相关代码:

 

 

 

 
 public class TestThread {
         public static void main(String[] args) {
                 MyThread1 mt = new MyThread1();
                 MyThread2 mt2 = new MyThread2();
                 mt.start();
                 new Thread(mt2).start();
                 new Thread() {//内部类实现
                         public void run() {
                                 for (int i = 0; i < 100; i++) {
                                         System.out.println("++++线程3++++++" + i);
                                 }
                         }
                 }.start();
                 new Thread(new Runnable() {
                         
                         @Override
                         public void run() {
                                 for (int i = 0; i < 100; i++) {
                                         System.out.println("******线程4****" + i);
                                 }
                         }
                 }).start();
                 for (int i = 0; i < 100; i++) {
                         System.out.println("******主线程****" + i);
                 }
         }
 }
 /**
  * 
  * 四种方法实现线程的创建,两种运用的是内部类 一种是实现接口,一种是继承线程
  * 
  */
 class MyThread1 extends Thread {
         @Override
         public void run() {
                 for (int i = 0; i < 100; i++) {
                         System.out.println("++++线程一++++++" + i);
                 }
         }
 }
 class MyThread2 implements Runnable {
         @Override
         public void run() {
                 for (int i = 0; i < 100; i++) {
                         System.out.println("-----线程二-------" + i);
                 }
         }
 }

 

 

当想让线程暂时暂停,进入休眠状态,可以用Sleep实现,该方法是静态方法,可以直接被Thread引用。可以在方法中设置休眠的时间。下面通过一个例子来说明Sleep的用法

 




 import java.io.IOException;
 import java.nio.CharBuffer;


 public class TestSleep {
         public static void main(String[] args) {
                 Runner r = new Runner();
                 Thread t1 = new Thread(r);
                 Thread t2 = new Thread(r);
                 t1.start();
                 t2.start();
         }
 }
 class Runner implements Runnable {
         @Override
         public void run() {
                 for (int i = 0; i < 200; i++) {
                         if (i % 10 == 0 && i != 0) {
                                 try {
                                         Thread.sleep(1000); //Sleep的用法
                                 } catch (InterruptedException e) {
                                         e.printStackTrace();
                                 }
                         }
                         System.out.println("——————————线程————————————" + i);
                 }
         }
 }
 

当想让一个线程先执行完再去执行另一个线程时,可以用Join方法将当前线程先执行完。下面通过一个例子来说明Join的用法。



 public class TestJoin {
         public static void main(String[] args) {
                 MyThread2 t1 = new MyThread2("zhangcun");
                 t1.start();
                 try {
                     t1.join();//先执行调用join方法的线程对象的run方法,完成后才调用这条语句所在的线程
                 } catch (InterruptedException e) {
                         e.printStackTrace();
                 }
 
                 for (int i = 1; i <= 10; i++) {
                         System.out.println("------i am main thread");
                 }
         }
 }
 class MyThread2 extends Thread {  
         MyThread2(String s) {
                 super(s);
         }
         public void run() {
                 for (int i = 1; i <= 10; i++) {
                         System.out.println("I am " + getName());
                         try {
                                 sleep(1000);
                         } catch (InterruptedException e) {
                                 System.out.println("又被打爆了!");
                                 return;
                         }
                 }
         }
 }
 

当需要一个线程让出当前执行状态即CPU,给其他线程执行的机会,就需要使用Yield方法,使当前线程暂时阻塞,让程序去执行其他的线程。还是通过一个例子来说明。

 

 



 public class TestYield {
         public static void main(String[] args) {
                 MyYield my1 = new MyYield();
                 MyYield2 my2 = new MyYield2();
                 my1.start();
                 my2.start();
 
         }
 }
 class MyYield extends Thread {
         @Override
         public void run() {
                 for (int i = 0; i < 100; i++) {
                         System.out.println("数字为:" + i);
                         if (i % 10 == 0 && i != 0) {
                                 yield();
                         }
                 }
         }
 }
 class MyYield2 extends Thread {
         @Override
         public void run() {
                 for (int i = 0; i < 100; i++) {
                         System.out.println("线程二数字为:" + i);
                         if (i % 10 == 0 && i != 0) {
                                 yield();
                         }
                 }
         }
 }

 

判断当前线程是否还在执行可以使用Thread.currentThread().isAlive()实现。

如果想让某一个线程的优先级优先,可以通过setPriority来设置线程的优先级。当然也会有一个getPriority来获取优先级MinPriority=1,MaxPriority=10,NomPriority=5。下面通过一个例子来说明setPriority的用法。

 



 public class TestPriority {
         public static void main(String[] args) {
                 T1 t = new T1();
                 T2 tt = new T2();
                 tt.setPriority(Thread.NORM_PRIORITY + 4);//设置线程的优先级来改变线程的优先级
                 t.start();
                 tt.start();
         }
 }
 class T1 extends Thread {
 
         @Override
         public void run() {
                 for (int i = 0; i < 100; i++) {
                         System.out.println("线程T1" + i);
                 }
         }
 }
 class T2 extends Thread {
 
         @Override
         public void run() {
                 for (int i = 0; i < 100; i++) {
                         System.out.println("--------线程T2" + i);
                 }
         }
 }
 

 

如果想实现当一个线程在占用一个资源时,不让别的线程来抢占资源,可以使用synchronized来修饰方法或者语句块,这样别的线程就不会进入synchronized修饰的方法或者方法块。

wait()和sleep()的区别:

<1>wait()时别的线程可以访问锁定对象。

<2>调用该方法时必须锁定该对象。

<3>sleep()时别的线程不可以访问锁定对象。

如果两个线程互相占用对方资源,那么线程将会进入死锁状态,在实现线程时,应该尽量避免死锁情况。下面就是一个典型的死锁例子,在编程时,我们应该要避免死锁的发生。

死锁例子:

 



 public class TestDeadLock implements Runnable {
         public int flag = 1;
         static Object o1 = new Object(), o2 = new Object();
         @Override
         public void run() {
                 System.out.println("flag=" + flag);
                 if (flag == 1) {
                         synchronized (o1) {
                                 try {
                                         Thread.sleep(500);
                                 } catch (Exception e) {
                                         e.printStackTrace();
                                 }
                                 synchronized (o2) {
                                         System.out.println("1");
                                 }
                         }
                 }
                 if (flag == 0) {
                         synchronized (o2) {
                                 try {
                                         Thread.sleep(500);
                                 } catch (Exception e) {
                                         e.printStackTrace();
                                 }
                                 synchronized (o1) {
                                         System.out.println("0");
                                 }
                         }
                 }
         }
         public static void main(String[] args) {
                 TestDeadLock td1 = new TestDeadLock();
                 TestDeadLock td2 = new TestDeadLock();
                 td1.flag = 1;
                 td2.flag = 0;
                 Thread t1 = new Thread(td1);
                 Thread t2 = new Thread(td2);
                 t1.start();
                 t2.start();
         }
 }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值