Java Thread Join方法
2011-08-30 21:03
477 查看
In this segment of tutorial we will learn how to use the join method in the Thread.Then We will create an example of Thread with the use of join method.
Java Join method join the next thread at the end of the currentthread
After current thread stops execution then next thread executes.
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 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进阶 线程池 join用法总结:thread4.join();方法,就表明thread4.join();这个线程受到贵客待遇,直到这个线程执行完,被插入这个方法的载体线程才可以执行。
- Java中的线程Thread方法之---join()
- java Thread的join()方法
- Java Thread.join()方法
- 浅析Java的Thread.join()方法
- java中Thread类的join方法
- Java Thread.join()方法
- [置顶] java Thread Join方法学习(同步)
- JAVA THREAD.JOIN方法详解
- Java多线程(一)——Thread之join()方法
- Java Thread.join()方法
- Java 多线程CountDownLatch、CyclicBarrier、Thread.join方法基本用法
- Java基础 thread的join方法
- java中synchronize锁 volatile thread.join()方法的使用
- Java多线程学习 (二) Thread的join()方法
- Java之Thread的join方法实例
- java基础知识回顾之java Thread类学习(十一)--join方法的理解
- Java的Thread的start(),run()和join()方法
- Java中的线程Thread方法之---join()