码迷,mamicode.com
首页 > 编程语言 > 详细

java线程操作范例

时间:2015-08-11 23:34:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:java

一:线程操作范例(1

package com.cloud.day6;

 

public class Demo1 {

         publicstatic void main(String[] args) {

                   MyThreadmt1=new MyThread("线程A", 1000);

                   MyThreadmt2=new MyThread("线程B", 2000);

                   MyThreadmt3=new MyThread("线程C", 3000);

                   mt1.start();

                   mt2.start();

                   mt3.start();

         }

}

class MyThread extends Thread{

         privateint time;

         publicMyThread(String name,int time){

                   super(name);

                   this.time=time;

         }

         publicvoid run(){

                   try{

                            Thread.sleep(this.time);

                   }catch (Exception e) {

                            e.printStackTrace();

                   }

                   System.out.println(Thread.currentThread().getName()+",休眠"+this.time);

         }

}

 

二:线程操作范例(2

 

package com.cloud.day6;

 

public class Demo1 {

         publicstatic void main(String[] args) {

                   MyThreadmt1=new MyThread("线程A", 1000);

                   MyThreadmt2=new MyThread("线程B", 2000);

                   MyThreadmt3=new MyThread("线程C", 3000);

                   newThread(mt1).start();

                   newThread(mt2).start();

                   newThread(mt3).start();

         }

}

class MyThread implements Runnable{

         privateString name;

         privateint time;

         publicMyThread(String name,int time){

                   this.name=name;

                   this.time=time;

         }

         publicvoid run(){

                   try{

                            Thread.sleep(this.time);

                   }catch (Exception e) {

                            e.printStackTrace();

                   }

                   System.out.println(this.name+",休眠"+this.time);

         }

}

 

三:线程中的同步售票问题

package com.cloud.day6;

 

public class Demo1 {

         publicstatic void main(String[] args) {

                   MyThreadmt1=new MyThread();

                   Threadt1=new Thread(mt1);

                   Threadt2=new Thread(mt1);

                   Threadt3=new Thread(mt1);

                   t1.start();

                   t2.start();

                   t3.start();

         }

}

class MyThread implements Runnable{

         privateint ticket=5;

         publicvoid run(){

                   for(inti=0;i<100;i++){

                            this.sale();

                   }

         }

         publicsynchronized void sale(){

                   if(ticket>0){

                            try{

                                     Thread.sleep(3000);

                            }catch (InterruptedException e) {

                                     e.printStackTrace();

                            }

                            System.out.println("卖了一张票,剩下票:"+(--ticket)+"张");

                   }

         }

}

 

四:线程死锁问题

即:两个线程都在等待对方执行

packagecom.cloud.day6;

 

publicclass Demo2 {

       public static void main(String[] args) {

              MyThread1 mt1=new MyThread1();

              MyThread1 mt2=new MyThread1();

              mt1.flag=true;

              mt2.flag=false;

              Thread th1=new Thread(mt1);

              Thread th2=new Thread(mt2);

              th1.start();

              th2.start();

       }

}

classZhansan{

       public void say(){

              System.out.println("张三:你给我画,我给你书");

       }

       public void get(){

              System.out.println("张三得到画了");

       }

}

classLisi{

       public void say(){

              System.out.println("李四:你给我书,我给你画");

       }

       public void get(){

              System.out.println("李四得到书了");

       }

}

classMyThread1 implements Runnable{

       private static Zhansan zs=new Zhansan();

       private static Lisi is=new Lisi();

       public boolean flag=false;

       @Override

       public void run() {

              if(flag){

                     synchronized (zs) {

                            zs.say();

                            try {

                                   Thread.sleep(500);

                            } catch(InterruptedException e) {

                                   e.printStackTrace();

                            }

                            synchronized (is) {

                                   zs.get();

                            }

                     }

              }

              else{

                     synchronized (is) {

                            is.say();

                            try {

                                   Thread.sleep(500);

                            } catch(InterruptedException e) {

                                   e.printStackTrace();

                            }

                            synchronized (zs) {

                                   is.get();

                            }

                     }

              }

       }

}

 

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

java线程操作范例

标签:java

原文地址:http://blog.csdn.net/dzy21/article/details/47427719

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!