閱讀365 返回首頁    go 京東網上商城


java線程學習1——線程基本概念和操作

 

一、創建線程的兩種方式

 

1 繼承Runnable接口

public class ThreadImpRunnable implements Runnable
{
 /**
  * 線程運行時執行的方法
  */
 public void run()
 {
  for (int i = 0; i < 500; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }

}

 

public class Test
{
 /**
  * 主線程,啟動線程必須是start方法
  */
 public static void main(String[] args)
 {
  ThreadImpRunnable tr = new ThreadImpRunnable();
  Thread t = new Thread(tr);
   t.start();

  for (int i = 0; i < 500; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }
}

 

 

2 繼承Thread類

public class ThreadExtends extends Thread
{
 /**
  * 線程運行時執行的方法
  */
 public void run()
 {
  for (int i = 0; i < 500; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }

}

 

public class Test
{
 /**
  * 主線程,啟動線程必須是start方法
  */
 public static void main(String[] args)
 {
  
ThreadExtends tr = new ThreadExtends();
  tr.start();

  for (int i = 0; i < 500; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }
}

 

 

 

二、線程的一些方法

1 sleep

public class SleepThread implements Runnable
{
 /**
  * 線程運行時執行的方法
  */
 public void run()
 {
  try
  {
   // 該線程進入阻塞狀態5秒
   
Thread.sleep(5000);
   for (int i = 0; i < 500; i++)
   {
    System.out.println(Thread.currentThread().getName() + i);
   }
  }
  catch (InterruptedException e)
  {
   // 若調用該線程的interrupt方法,會報該異常,真實程序中可以關閉一些資源
   e.printStackTrace();
  }
 }
}

 

public class SleepTest
{
 /**
  * 主線程
  */
 public static void main(String[] args)
 {
  SleepThread tr = new SleepThread();
  Thread t = new Thread(tr);
  t.start();
  for (int i = 0; i < 500; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }
}

 

2 Join

public class JoinThread implements Runnable
{
 public void run()
 {
  for (int i = 0; i < 100; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }
}

 

public class JoinTest
{
 public static void main(String[] args)
 {
  JoinThread jt = new JoinThread();
  Thread t = new Thread(jt);
  t.start();
  try
  {
   // 調用該方法將當前線程(此處是主線程)合並到本線程中,
執行完本線程,再執行當前線程
   
t.join();
  }
  catch (InterruptedException e)
  {
   e.printStackTrace();
  }
  for (int i = 0; i < 100; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }

}

 

 

3 yield

public class YieldThread extends Thread
{
 public void run()
 {
  for (int i = 0; i < 100; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
   if (i % 10 == 0)
   {
    // 當能被10整除時本線程讓出優先級,讓其他線程先執行一會,可看到t1_10下麵緊接著t2的結果,同樣t2_20下麵緊接著t1的結果
   
 yield();
   }
  }
 }
}

 

public class YieldTest
{
 public static void main(String[] args)
 {
  YieldThread yt = new YieldThread();
  Thread t1 = new Thread(yt, "t1_");
  t1.start();

  Thread t2 = new Thread(yt, "t2_");
  t2.start();
 }
}

 

4 setPriority

public class PriorityThread extends Thread
{
 public void run()
 {
  for (int i = 0; i < 100; i++)
  {
   System.out.println(Thread.currentThread().getName() + i);
  }
 }
}

 

public class PriorityTest
{
 /**
  * 線程優先級默認是5
  */
 public static void main(String[] args)
 {
  int norm = Thread.NORM_PRIORITY; // 5
  int max = Thread.MAX_PRIORITY;   // 10
  int min  = Thread.MIN_PRIORITY;  // 1

  PriorityThread yt = new PriorityThread();
  Thread t1 = new Thread(yt, "t1_");
  t1.setPriority(norm + 3);
  t1.start();

  Thread t2 = new Thread(yt, "t2_");
  t2.start();
 }
}

 

 

最後更新:2017-04-04 07:32:09

  上一篇:go jdk1.5新特性5之枚舉之模擬枚舉類型
  下一篇:go 優秀項目經理的七個習慣