线程的常用方法

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. Oracle数据库用户及表空间操作

    //创建数据表空间 create tablespace test_data datafile '/u01/user_data.dbf' size 50m autoextend on next 50m ...

  2. 根据字段表 自动创建 表SQL

    create PROC CreateTableSql ) AS ) ,) ,) ) ,) ,) ,@IsIdentity bit ,@IsNull bit ,@IsPrimaryKey bit ),) ...

  3. Python3.6全栈开发实例[014]

    14.好声音选秀大赛评委在打分的时,可以进行输入. 假设,有10个评委.让10个评委进行打分, 要求, 分数必须大于5分, 小于10分. count = 1 while count <= 10: ...

  4. LeetCode::Sort List 具体分析

    Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话.给链表排序,看到nlogn.我们 ...

  5. 微信公众号的搭建-第四天(2)-获取并缓存access_token

    1. 什么是access_token? 为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台 开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等 ...

  6. Some day some time we will do

    Age has been reached the end of the beginning of the world,May be guilty in his seems to passing a l ...

  7. 解决问题知识点--mysql数据库

    一: 连接 多表的字段中含null的  多表查询 ----左右连接查询! 例: 一张  emp(emano,ename,sal,deptno) 雇员表, 一张 dept(deptno,dname) 部 ...

  8. 两张Excel表比较,两个for循环比较优化使用Contains

    将一个Excel表中的sheet中的一列导出到List<string>,用一个for循环循环另一张表中的数据,看是否在第一张表中的Contains中

  9. hibernate 操作 Postgresql 数据库报 operator does not exist: integer = character varying

    网上的说法如下: Java开发Postgresql 数据库兼容应用的问题,与Oracle有一些不同: Java类型映射数据库类型的不同,Oracle jdbc驱动程序处理Java String类型可正 ...

  10. 2015.7.2 想做T再次失败

    2015.7.2教训:不要心存侥幸! 1.昨天收盘急跌,加上看到成交量在增加,负荷庄家行为第五条,一时脑热就去抄了.其实在震荡行情下,第二天肯定有时间点比头一天的收盘价低(Pic1) 2.T+0原则: ...