多线程(join和yield)
来源:互联网 发布:js 特殊字符校验 编辑:程序博客网 时间:2024/06/08 11:38
/*
* 一.join:
* 当A线程执行到了b线程的join方法时,A线程就会等待,等B现成都执行完,A才会执行。
* 返回线程的:线程名,优先级,线程组。
* 优先级:抢资源的频率。
* 线程可以具有的最高优先级。
* static int MAX_PRIORITY (10)
* 线程可以具有的最低优先级。
* static int MIN_PRIORITY (1)
* 分配给线程的默认优先级。
暂停当前正在执行的线程对象,并执行其他线程。
*/
class Demo implements Runnable
{
public void run()
{
for(int x=0;x<70;x++)
{
System.out.println(Thread.currentThread().toString()+"----"+x);
Thread.yield();
}
}
}
public class JoinDemo {
public static void main(String args[]) throws InterruptedException
{
Demo d=new Demo();
Thread t1=new Thread(d);
Thread t2=new Thread(d);
t1.start();
//t1.setPriority(Thread.MAX_PRIORITY);
t2.start();
//t1.join();
for(int x=0;x<80;x++)
{
//System.out.println("main----"+x);
}
System.out.println("over");
}
* 一.join:
* 当A线程执行到了b线程的join方法时,A线程就会等待,等B现成都执行完,A才会执行。
* join可一用来临时加入线程执行。
* 返回线程的:线程名,优先级,线程组。
* 优先级:抢资源的频率。
* 线程可以具有的最高优先级。
* static int MAX_PRIORITY (10)
* 线程可以具有的最低优先级。
* static int MIN_PRIORITY (1)
* 分配给线程的默认优先级。
* static int NORM_PRIORITY (5)
暂停当前正在执行的线程对象,并执行其他线程。
*/
class Demo implements Runnable
{
public void run()
{
for(int x=0;x<70;x++)
{
System.out.println(Thread.currentThread().toString()+"----"+x);
Thread.yield();
}
}
}
public class JoinDemo {
public static void main(String args[]) throws InterruptedException
{
Demo d=new Demo();
Thread t1=new Thread(d);
Thread t2=new Thread(d);
t1.start();
//t1.setPriority(Thread.MAX_PRIORITY);
t2.start();
//t1.join();
for(int x=0;x<80;x++)
{
//System.out.println("main----"+x);
}
System.out.println("over");
}
}
//开发中使用多线程,为了同时运行代码,如:
public class ThreadTest {
public static void main(String args[])
{
new Thread()
{
public void run()
{
for(int x=0;x<100;x++)
{
System.out.println(Thread.currentThread().getName()+"---"+x);
}
}
}.start();
for(int x=0;x<100;x++)
{
System.out.println(Thread.currentThread().getName()+"---"+x);
}
Runnable r=new Runnable()
{
public void run()
{
for(int x=0;x<100;x++)
{
System.out.println(Thread.currentThread().getName()+"---"+x);
}
}
};
new Thread(r).start();
}
}
0 0
- 多线程(join和yield)
- 【多线程】yield()和join()等方法的使用
- Java多线程之sleep,wait,join和yield关键字
- sleep、yield和join
- sleep和yield,join
- join 和 yield
- Java多线程-(5)线程的优先级、sleep()、yield()、join()
- Java多线程:(三)一些方法:start(),sleep(),yield(),join()
- yield方法和join方法
- Java 线程 | yield() 和 join()
- Java多线程-线程状态、sleep()、yield()、join()
- java多线程之join与yield
- 多线程中的join(),yield()与优先级Priority
- java多线程之join与yield
- 多线程的sleep,join,yield,wait,notify。
- java 多线程(2) 线程的调度和优先级 / sleep() / interrupt() 打断join() / yield() / setPriority()
- 多线程(2)-- yield( )
- 黑马程序员——多线程(Lock、setDaemon、join、setPriority、yield)
- hdu5319 Painter
- malloc(0)
- 机试算法讲解:第50题 动态规划之拦截导弹
- poj-3083 Children of the Candy Corn-DFS+BFS
- leetcode--Kth Smallest Element in a BST
- 多线程(join和yield)
- Lucene七(搜索)
- Servlet学习
- NYOJ269VF【dp】
- 回溯法解决N皇后问题
- java.lang.ClassNotFoundException: Didn't find class "*****Activity" on path: /data/app/*******.apk
- HDU 1203
- 6.地址,Block,SEL,多态
- 你是ACM吗?