线程的实现

package cn.yq.java.xiancheng2;
public class Runnabl {

public static void main(String[] args) {

MyRunnable1 st = new MyRunnable1();
 Thread t1 = new Thread(st, "1号窗口");
 Thread t2 = new Thread(st, "2号窗口");
 Thread t3 = new Thread(st, "3号窗口");
 Thread t4 = new Thread(st, "4号窗口 ");
 t1.setPriority(7);
 t2.setPriority(8);
 t3.setPriority(9);
 t4.setPriority(10);
 t1.start();
 t2.start();
 t3.start();
 t4.start();
}
}


============================================

package cn.yq.java.xiancheng2;


public  class MyRunnable1 implements Runnable {

int tickets = 500;
public void run(){

  while(tickets > 0){
  sale();
 
  }
 
}


private synchronized void sale(){
 if(tickets > 0){
  System.out.println(Thread.currentThread().getName() +"\t卖出 第 "+ (500 - tickets + 1)+"张票");
  tickets--;
  
 try{
   Thread.sleep(500);
  }catch(InterruptedException e){
   e.printStackTrace();
  }
 }
}
}

================================================

package cn.yq.java.xiancheng2;


public class Threa1 {


public static void main(String[] args) {
MyThread1 myth1=new MyThread1("Myth-01");
MyThread1 myth2=new MyThread1("Myth-02");
MyThread1 myth3=new MyThread1("Myth-03");
MyThread1 myth4=new MyThread1("Myth-04");
MyThread1 myth5=new MyThread1("Myth-05");
MyThread1 myth6=new MyThread1("Myth-06");
//同步  随机  (myth1.start报错)
myth1.start();
myth2.start();
//睡眠  
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myth3.start();
myth4.start();
myth5.start();
myth6.start();


}


}

=================================================

package cn.yq.java.xiancheng2;


public class MyThread1 extends Thread  {

String name;
int age= 10;
MyThread1(String name){
this.name = name;
}
public void run(){
for(int i = 0;i<20;i++){
if(age>0){
System.out.println("继承Thread的线程:"+Thread.currentThread().getName()+"\tage:"+age--);
}
}

}
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值