????????????????е???yield()????????μ??????
I am Consumer : Consumed Item 0
I am Consumer : Consumed Item 1
I am Consumer : Consumed Item 2
I am Consumer : Consumed Item 3
I am Consumer : Consumed Item 4
I am Producer : Produced Item 0
I am Producer : Produced Item 1
I am Producer : Produced Item 2
I am Producer : Produced Item 3
I am Producer : Produced Item 4
?????????????????yield()????????μ??????
I am Producer : Produced Item 0
I am Consumer : Consumed Item 0
I am Producer : Produced Item 1
I am Consumer : Consumed Item 1
I am Producer : Produced Item 2
I am Consumer : Consumed Item 2
I am Producer : Produced Item 3
I am Consumer : Consumed Item 3
I am Producer : Produced Item 4
I am Consumer : Consumed Item 4
????join()????
???????????????join()????????????????????????н??????????????????????join()?????????????????????????????????????????????????????????С?
????//Waits for this thread to die.
????public final void join() throws InterruptedException
??????join()???????趨????????join()?????????????????????Ч????????????????????????????????е???????????????????漰sleep???join()???????????????????????????ü??join()????????????????????
??????sleep??join??????InterruptedException???ж??????????
????join()??????????
package test.core.threads;
public class JoinExample
{
public static void main(String[] args) throws InterruptedException
{
Thread t = new Thread(new Runnable()
{
public void run()
{
System.out.println("First task started");
System.out.println("Sleeping for 2 seconds");
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("First task completed");
}
});
Thread t1 = new Thread(new Runnable()
{
public void run()
{
System.out.println("Second task completed");
}
});
t.start(); // Line 15
t.join(); // Line 16
t1.start();
}
}
Output:
First task started
Sleeping for 2 seconds
First task completed
Second task completed
?????????Щ??С?????????????????????????????????