[转载]JAVA线程编程

本文通过多个示例介绍Java线程编程的基本概念和技术,包括线程的创建、后台线程设置、线程合并、同步机制等核心内容。通过具体代码演示了如何利用Thread类和Runnable接口来创建和管理线程,以及如何解决多线程环境中资源并发访问的问题。
原文地址:JAVA线程编程 作者:illool

//多线程和单线程
class threaddemo1
{
 public static void main(String [] args)
 {  
   new testthread().start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   while(true)
   {
     System.out.println("main:"+Thread.currentThread().getName());
   }
 } 
}

class testthread extends Thread    //创建Thread子类,重写run()方法;
{
  public void run()
  {
    while(true)
    {
      System.out.println("run:"+Thread.currentThread().getName());
    }
  }
}



 

 

 

//后台线程
class threaddemo11
{
 public static void main(String [] args)
 {  
   Thread tt = new testthread();
   tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   
 } 
}

class testthread extends Thread    //创建Thread子类,重写run()方法;
{
  public void run()
  {
    while(true)
    {
      System.out.println("run:"+Thread.currentThread().getName());
    }
  }
}

 

 

//join()方法
class threaddemo12
{
 public static void main(String [] args)
 {  
   Thread tt = new testthread();
   //tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   int index = 0;
   while(true)
   {
     if(index++ == 100)
          try{tt.join(10000);}catch(Exception e){}//循环100次以后,子类tt中的线程合并到住线程中去
     System.out.println("main:"+Thread.currentThread().getName());
   }
 } 
}

class testthread extends Thread    //创建Thread子类,重写run()方法;
{
  public void run()
  {
    while(true)
    {
      System.out.println("run:"+Thread.currentThread().getName());
    }
  }
}

 

 

 

//不使用Runnable 的接口对象的创建,但创建4个独立的线程
class threaddemo13
{
 public static void main(String [] args)
 {  
   //Thread tt = new testthread();
   //tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   //Thread tt = new Thread(new testthread());//创建一个线程对象,并传入一个Runnable的线程对象;将不在自动的调用Thread
                       //类中自带的run()方法了,而调用Runnable对象中的run()方法; 
   //tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   
   new testthread().start();//产生4个线程
   new testthread().start();
   new testthread().start();
   new testthread().start();
 } 
}

class testthread extends Thread //用直接继承Thread的方式
                  //创建一个Runnable的接口类的对象 
                  //创建Thread子类,重写run()方法;
{
  int tickets = 100;
  public void run()
  {
    while(true)
    {
     if(tickets>0)
      System.out.println("run:"+Thread.currentThread().getName() + "出售的票号码:" + tickets--);
    }
  }
}

 

 

//不使用Runnable 的接口对象的创建,在同一个线程对象中创建4个线程
class threaddemo14
{
 public static void main(String [] args)
 {  
   //Thread tt = new testthread();
   //tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   //Thread tt = new Thread(new testthread());//创建一个线程对象,并传入一个Runnable的线程对象;将不在自动的调用Thread
                       //类中自带的run()方法了,而调用Runnable对象中的run()方法; 
   //tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   
   testthread tt = new testthread();//产生一个对象,并启动4个线程,实现拥有共同的tickets=100
   tt.start();           //实际只有一个线程在工作
   tt.start();
   tt.start();
   tt.start();
 } 
}

class testthread extends Thread//创建一个Runnable的接口类的对象   
                  //创建Thread子类,重写run()方法;
{
  int tickets = 100;
  public void run()
  {
    while(true)
    {
      if(tickets>0)
      System.out.println("run:"+Thread.currentThread().getName() + "出售的票号码:" + tickets--);
    }
  }
}

 

 

//Runnable 的接口对象的创建
class threaddemo15
{
 public static void main(String [] args)
 {  
   //Thread tt = new testthread();
   //tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   //Thread tt = new Thread(new testthread());//创建一个线程对象,并传入一个Runnable的线程对象;将不在自动的调用Thread
                       //类中自带的run()方法了,而调用Runnable对象中的run()方法; 
   //tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   
   testthread tt = new testthread();//产生一个对象,这个对象是以Runnable实现的
   
   new Thread(tt).start();   //再将Runnable的对象传到Thread里去;
   new Thread(tt).start();
   new Thread(tt).start();
   new Thread(tt).start();
 } 
}

class testthread implements Runnable//extends Thread//创建一个Runnable的接口类的对象   
                  //创建Thread子类,重写run()方法;
{
  int tickets = 100;
  public void run()
  {
    while(true)
    {
      if(tickets>0)
      System.out.println("run:"+Thread.currentThread().getName() + "出售的票号码:" + tickets--);
    }
  }
}

 

 

//多线程的安全,防止资源被同时的使用:synchronized(str),是以牺牲程序上午性能为代价的
class threaddemo16
{
 public static void main(String [] args)
 {  
   //Thread tt = new testthread();
   //tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   //Thread tt = new Thread(new testthread());//创建一个线程对象,并传入一个Runnable的线程对象;将不在自动的调用Thread
                       //类中自带的run()方法了,而调用Runnable对象中的run()方法; 
   //tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   
   testthread tt = new testthread();//产生一个对象,这个对象是以Runnable实现的
   
   new Thread(tt).start();   //再将Runnable的对象传到Thread里去;
   new Thread(tt).start();
   new Thread(tt).start();
   new Thread(tt).start();
 } 
}

class testthread implements Runnable//extends Thread//创建一个Runnable的接口类的对象   
                  //创建Thread子类,重写run()方法;
{
  int tickets = 100;
  String str = new String ("");   //必须定义在run()方法的外面;如定义在里面,4个线程就会创建4个str对象;
  public void run()
  {
    while(true)
    {
      synchronized(str)  //synchronized(str)的括号中必须是一个对象,可以任意的定义一个对象,实现了同步;
                //检查str的标志位;叫同步代码块
      {
          if(tickets>0)
          {
            try{Thread.sleep(10);}catch(Exception e){};//让线程暂停10mm,最终出现异常。
            System.out.println("run:"+Thread.currentThread().getName() + "出售的票号码:" + tickets--);
            //在一个线程在执行完tickets>0以后,其他的线程不可以执行if里面的代码;
          }
      }
    }
  }
}

 

 

 

//多线程的安全,防止资源被同时的使用:synchronized(str),是以牺牲程序上午性能为代价的
//使用同步函数实现同步;
class threaddemo17
{
 public static void main(String [] args)
 {  
   //Thread tt = new testthread();
   //tt.setDaemon(true);  //setDaemon(true)方法,这个线程就变成了后台线程
   //Thread tt = new Thread(new testthread());//创建一个线程对象,并传入一个Runnable的线程对象;将不在自动的调用Thread
                       //类中自带的run()方法了,而调用Runnable对象中的run()方法; 
   //tt.start();//run();
   //start()方法启动后会调用run()方法,run()是个空的函数;
   
   testthread tt = new testthread();//产生一个对象,这个对象是以Runnable实现的
   
   new Thread(tt).start();   //再将Runnable的对象传到Thread里去;
   try{Thread.sleep(1);}catch(Exception e){}  //让CPU暂停,则又开始了上一条语句所指示的线程开始执行;
   tt.str = new String("method");
   new Thread(tt).start();
   new Thread(tt).start();
   tt.str = new String("method");
   new Thread(tt).start();
 } 
}

class testthread implements Runnable//extends Thread//创建一个Runnable的接口类的对象   
                  //创建Thread子类,重写run()方法;
{
  int tickets = 100;
  String str = new String ("");   //必须定义在run()方法的外面;如定义在里面,4个线程就会创建4个str对象;
  public void run()
  {
    if(str.equals("method"))
    {
        while(true)
        {
          synchronized(str)  //synchronized(str)的括号中必须是一个对象,可以任意的定义一个对象,实现了同步;
                    //检查str的标志位;叫同步代码块
          {
              if(tickets>0)
              {
                try{Thread.sleep(10);}catch(Exception e){};//让线程暂停10mm,最终出现异常。
                System.out.println("run:"+Thread.currentThread().getName() + "出售的票号码:" + tickets--);
                //在一个线程在执行完tickets>0以后,其他的线程不可以执行if里面的代码;
              }
          }
        }
    }
    else
    {
       while(true)
       {
         sale();
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值