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 < 10; 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.java
administrator@xu-desktop:~$ java 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(); // 获取当前线程
对线程的名字一般是在启动前设定,最好不要设置相同的名字,最好不要为一个线程该名字;
java是多线程操作语言,当你执行java命令时,至少运行了两个线程:1.主线程,主方法生成的,2.gc线程,垃圾回收机制,
他们都包含在进程java中,
每次执行java命令后,OS都将启动一个JVM的进程,主方法只是这个进程上进一步的划分;
-
package com.mldn;
-
-
class ThreadFun implements Runnable
-
{
-
public void run()
-
{
-
for (int i = 0; i < 100; i++)
-
{
-
System.out.println(Thread.currentThread().getName() + “线程正在运行!”);
-
}
-
}
-
}
-
-
public class ThreadName
-
{
-
public static void main(String[] args)
-
{
-
ThreadFun thread = new ThreadFun();
-
-
Thread th1 = new Thread(thread, “A”);
-
Thread th2 = new Thread(thread, “B”);
-
Thread th3 = new Thread(thread, “C”);
-
-
th1.start();
-
th2.start();
-
th3.start();
-
thread.run(); // 主线程
-
}
-
}
复制代码
———–线程的休眠:
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 < 10; 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.java
-
administrator@xu-desktop:~$ java 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将同时并发运行,呈蹦跳式有节奏地运行!
-
看来休眠的使用,使交互式操作更完美!
-
*/
复制代码
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《Java线程理解-线程的休眠》
本文地址:http://www.xiupu.net/archives-68.html
关注公众号:
微信赞赏
支付宝赞赏