Java Thread Join方法
来源:互联网 发布:大麦盒子怎么设置网络 编辑:程序博客网 时间:2024/06/10 19:56
- Java Join method join the next thread at the end of the currentthread
- After current thread stops execution then next thread executes.
public class join implements Runnable { @Override public void run() { for (int x = 1; x <= 3; x++) { System.out.println("this is thread " + Thread.currentThread().getName()); } } public static void main(String[] args) throws Exception { join j1 = new join(); Thread t1 = new Thread(j1, "1"); Thread t2 = new Thread(j1, "2"); Thread t3 = new Thread(j1, "3"); t1.start(); t1.join(); t2.start(); t3.start(); }}
Output :
this is thread 1this is thread 1this is thread 1this is thread 3this is thread 2this is thread 2this is thread 2this is thread 3this is thread 3可以在t2.start后面再 加个t2.join()看看区别。
再列个例子:
using System;
namespace TestThreadJoin
{
class Program
{
static void Main()
{
System.Threading.Thread x = new System.Threading.Thread(new System.Threading.ThreadStart(f1));
x.Start();
Console.WriteLine("This is Main.{0}", 1);
x.Join();
Console.WriteLine("This is Main.{0}", 2);
Console.ReadLine();
}
static void f1()
{
System.Threading.Thread y = new System.Threading.Thread(new System.Threading.ThreadStart(f2));
y.Start();
y.Join();
Console.WriteLine("This is F1.{0}",1);
}
static void f2()
{
Console.WriteLine("This is F2.{0}", 1);
}
}
}
这儿有三个线程在处理(包括主线程),大家可看看执行结果.
结果:
This is Main.1
This is F2.1
This is F1.1
This is Main.2
如果: 注释// x.Join();
结果:
This is Main.1
This is Main.2
This is F2.1
This is F1.1
- Java Thread Join方法
- Java Thread.join()方法
- java Thread join方法
- Java Thread.join方法
- java Thread的join()方法
- java--java.lang.Thread.join() 方法
- java中Thread类的join()方法
- java Thread方法中join()的用法
- Java中的线程Thread方法之---join()
- Java基础 thread的join方法
- java API------Thread类join()方法
- java中Thread类的join()方法
- Java中的线程Thread方法之---join()
- 浅析Java的Thread.join()方法
- Java中thread的join方法
- java Thread 线程的.Join()方法
- Java线程Thread.join方法解析
- java中Thread类的join方法
- POJ 1011
- sql 自动编号
- 如何寻找出色的产品经理
- via和pad的区别
- Stack(栈)和Heap(堆)的区别
- Java Thread Join方法
- 单例模式的几种实现及其比较
- 线性表中插入元素
- Linux系统命令及其使用详解
- Ubuntu Linux 8.04系统下智能拼音输入法
- hdu 2883(最大流)
- ASP.NET中MD5和SHA1的几种方法
- Java5泛型的用法,T.class的获取和为擦拭法站台
- 从百度文库看企业内部信息泄漏