Java线程理解-线程的休眠

 

public static void sleep(long millis) throws InterruptedException; // 休眠xx毫秒;
1.使用使需要try catch 处理;

package com.mldn;

 

class Thr implements Runnable

{

public void run()

{

for (int i = 0; i

{

try

{

Thread.sleep(500); // 当前线程休眠500毫秒

}

catch (InterruptedException e)

{

e.printStackTrace();

}

 

System.out.println(Thread.currentThread().getName() + ” 线程正在运行!”);

}

}

}

 

public class ThreadSleep

{

public static void main(String[] args)

{

Thr th = new Thr();

 

Thread th1 = new Thread(th, “A”);

Thread th2 = new Thread(th, “B”);

 

th1.start();

th2.start();

}

}

/*

administrator@xu-desktop:~$ javac -d . ./work/ThreadSleep.

administrator@xu-desktop:~$ com.mldn.ThreadSleep

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

A 线程正在运行!

B 线程正在运行!

发现:A B将同时并发运行,呈蹦跳式有节奏地运行!

看来休眠的使用,使交互式操作更完美!

*/

——————–线程的操作方法:
线程,所有的操作方法在Thread类中定义;
线程可以设置名字,可以取得名字,可以获取当前线程;
1.设置线程名字:
public final void setName(String name);
构造方法:
public Thread(Runnable target, String name);
public Thread(String name);

2.取得名字:
public final String getName();
因线程操作的不确定性,提供了方法可以取得当前的操作线程;
pubic static Thread currentThread(); // 获取当前线程
对线程的名字一般是在启动前设定,最好不要设置相同的名字,最好不要为一个线程该名字;

是多线程操作语言,当你执行命令时,至少运行了两个线程:1.主线程,主方法生成的,2.gc线程,垃圾回收机制,
他们都包含在进程中,
每次执行java命令后,OS都将启动一个JVM的进程,主方法只是这个进程上进一步的划分;

  1. package com.mldn;
  2.  
  3. class ThreadFun implements Runnable
  4. {
  5.         public void run()
  6.         {
  7.                 for (int i = 0; i
  8.                 {
  9.                         System.out.println(Thread.currentThread().getName() + “线程正在运行!”);
  10.                 }
  11.         }       
  12. }
  13.  
  14. public class ThreadName
  15. {
  16.         public static void main(String[] args)
  17.         {
  18.                 ThreadFun thread = new ThreadFun();
  19.                
     
  20.                 Thread th1 = new Thread(thread, “A”);
  21.                 Thread th2 = new Thread(thread, “B”);
  22.                 Thread th3 = new Thread(thread, “C”);
  23.                
     
  24.                 th1.start();
  25.                 th2.start();
  26.                 th3.start();
  27.                 thread.run(); // 主线程
  28.         }
  29. }

复制代码

———–线程的休眠:
public static void sleep(long millis) throws InterruptedException; // 休眠xx毫秒;
1.使用使需要try catch 处理;

  1. package com.mldn;
  2.  
  3. class Thr implements Runnable
  4. {
  5.         public void run()
  6.         {
  7.                 for (int i = 0; i
  8.                 {
  9.                         try
  10.                         {
  11.                                 Thread.sleep(500);        // 当前线程休眠500毫秒
  12.                         }
  13.                         catch (InterruptedException e)
  14.                         {
  15.                                 e.printStackTrace();
  16.                         }
  17.                        
     
  18.                         System.out.println(Thread.currentThread().getName() + ” 线程正在运行!”);
  19.                 }
  20.         }
  21. }
  22.  
  23. public class ThreadSleep
  24. {
  25.         public static void main(String[] args)
  26.         {
  27.                 Thr th = new Thr();
  28.                
     
  29.                 Thread th1 = new Thread(th, “A”);
  30.                 Thread th2 = new Thread(th, “B”);
  31.                
     
  32.                 th1.start();
  33.                 th2.start();
  34.         }
  35. }
  36. /*
  37. administrator@xu-desktop:~$ javac -d . ./work/ThreadSleep.java
  38. administrator@xu-desktop:~$ java com.mldn.ThreadSleep
  39. A 线程正在运行!
  40. B 线程正在运行!
  41. A 线程正在运行!
  42. B 线程正在运行!
  43. A 线程正在运行!
  44. B 线程正在运行!
  45. A 线程正在运行!
  46. B 线程正在运行!
  47. A 线程正在运行!
  48. B 线程正在运行!
  49. A 线程正在运行!
  50. B 线程正在运行!
  51. A 线程正在运行!
  52. B 线程正在运行!
  53. A 线程正在运行!
  54. B 线程正在运行!
  55. A 线程正在运行!
  56. B 线程正在运行!
  57. A 线程正在运行!
  58. B 线程正在运行!
  59. 发现:A B将同时并发运行,呈蹦跳式有节奏地运行!
  60. 看来休眠的使用,使交互式操作更完美!
  61. */

复制代码

 

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Java线程理解-线程的休眠
本文地址:https://www.zhiletu.com/archives-68.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微