黑马程序员技术交流社区
标题:
线程优先级,join(),toString()和yield()方法和开发中的常用形式
[打印本页]
作者:
追逐
时间:
2014-3-19 12:51
标题:
线程优先级,join(),toString()和yield()方法和开发中的常用形式
本帖最后由 追逐 于 2014-3-21 19:34 编辑
/*
需求:
1.设置优先级
2.yield方法
和开发中常用的写线程的方式
*/
class YouXian implements Runnable
{
public void run()
{
for(int i = 0; i < 50; i++)
{
//toString(); //返回该线程的字符串表现形式,包括线程名称、优先级和线程组
System.out.println(Thread.currentThread().toString() + "...run..." + i);
//Thread.yield(); //暂停当前线程,释放执行权
}
}
}
class YouXianDemo
{
public static void main(String[] args)
{
/*//继承Thread类。使用匿名类写
new Thread()
{
public void run()
{
for(int i = 0; i < 50; i++)
{
System.out.println(Thread.currentThread().getName() + "...run..." + i);
}
}
}.start();
//写一个for循环
for(int i = 0; i < 30; i++)
{
System.out.println(Thread.currentThread().getName() + "main..............." + i);
}
//实现接口Runnable
Runnable r = new Runnable()
{
public void run()
{
for(int i = 0; i < 50; i++)
{
System.out.println(Thread.currentThread().getName() + "Runnable-------------------------" + i);
}
}
};*/
YouXian y = new YouXian();
Thread t1 = new Thread(y);
Thread t2 = new Thread(y);
t1.start();
//t1.setPriority(Thread.MAX_PRIORITY); //设置优先级
t2.start();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2