线程的常用方法

1.start()

线程调用该方法将启动线程,使之从新建状态进入就绪队列排队。

2.run()

3.sleep()

4.isAlive()

线程处于新建状态时,线程调用isAlive()方法返回false。

public class ClassRoom implements Runnable {
Thread student, teacher;
ClassRoom()
{
teacher = new Thread(this);
student = new Thread(this);
teacher.setName("王教授");
student.setName("张三");
} @Override
public void run() {
// TODO Auto-generated method stub
if(Thread.currentThread() == student)
{
System.out.println(student.getName() + "正在睡觉, 不听课");
try {
Thread.sleep(1000*100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println(student.getName() + "被老师叫醒了");
}
}
else if(Thread.currentThread() == teacher)
{
for(int i = 1; i <= 3; i++){
System.out.println("上课");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
student.interrupt();
}
} }

运行

public class Test03 {

    public static void main(String[] args) {
// TODO Auto-generated method stub
ClassRoom room6501 = new ClassRoom();
room6501.student.start();
room6501.teacher.start();
} }

运行结果如下所示:

review37的更多相关文章

随机推荐

  1. Toeplitz matrix

    w https://en.wikipedia.org/wiki/Toeplitz_matrix Proof of Stolz-Cesaro theorem | planetmath.org  http ...

  2. 【转】通过VIOS实现AIX系统的网络虚拟化

    在上一篇博文中,我们已经在一个新创建的LPAR中通过File-backed device以及VMLibrary的方式成功安装了一个AIX系统,接下来我们讨论如何通过VIOS的协助来完成新装AIX系统的 ...

  3. Java你不知道的那些事儿—Java隐藏特性

    转载自:http://www.cnblogs.com/lanxuezaipiao/p/3460373.html 每 种语言都很强大,不管你是像我一样的初学者还是有过N年项目经验的大神,总会有你不知道的 ...

  4. 整理前端css/js/jq常见问题及解决方法(2)

    移动端 手机 1.点击图片或按钮,选中状态影响到其他范围解决:html{-webkit-user-select:none}<meta name="msapplication-tap-h ...

  5. Python2 和 Python3 区别汇总

    [Python2 和 Python3 的区别汇总,不定期补充] print 在进行程序调试时用得最多的语句可能就是 print,在 Python 2 中,print 是一条语句,而 Python3 中 ...

  6. Python3 面向对象(1)

    面向.概述 面向过程: 根据业务逻辑从上到下写垒代码面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点: 极大降低了程序的 ...

  7. Clustered and Secondary Indexes

    Clustered and Secondary Indexes secondary index A type of InnoDB index that represents a subset of t ...

  8. Ubuntu Server 16.04安装xfce4图形界面远程控制

    1.首先连接上你的服务器,然后安装vncserver,命令如下 apt-get install vnc4server 2.安装图形界面 apt-get install xfce4如果安装不上,就 ap ...

  9. 一个用 C# 实现操作 XML 文件的公共类代码

    using System; using System.IO; using System.Data; using System.Xml; using System.Xml.XPath; namespac ...

  10. python常用模块-1

    一.认识模块 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.py文 ...