My mailbox: <kco1989@qq.com>.
Welcome to reprint. Please indicate the website for reprinting. http://blog.csdn.net/tianshi_kco
github: https://github.com/kco1989/kco
The code is fully hosted github Students in need download it by themselves
Introduction
Today we'll talk about Exchanger, the last synchronization tool class. It's relatively simple to have two threads exchange data.
theory
Exchanger has only two methods, and the two are the same, but the parameters are not clear.
exchange(V x) exchanges data x with another thread. If the data of another thread is ready, the current thread will return immediately and get the data of another thread; otherwise, the current thread will enter a waiting state.
V exchange(V x, long timeout, TimeUnit unit): With exchange, a TimeoutException exception is thrown if there is a specified time-out time, and if the waiting time is out of time and the other party's data is not received.
Examples include Patient Yamaguchi and Luffy
Everyone who has seen the King of Pirates knows Shanzhi and Lufei. Shanzhi is a cook and his craftsmanship is a bar. Lufei is a big stomach king and super-eatable. Now write a program to let Shanzhi keep making food for Lufei, while Lufei keeps eating. After eating, he should say thanks to Shanzhi. ok and start coding.
Write a Food food class first. It's simpler to define some foods.
public class Food { public final static String[] food = { "Beating Side Stove", "Milky Asparagus Soup", "Grains of Duck", "Roast Flowers and Sweet Osmanthus Fish", "Happy in Pain", "Seven Star Pills", "Duck Yellow Bean Curd", "Bess Chopped Vegetable Bile", "Crisp Fried Pumpkin Shredded", "Dragon and Phoenix Legs"," // Eliminate part of the code... }; private static Random random = new Random(); public static String getRandomFood(){ return food[random.nextInt(food.length)]; } }
After that, Shanzhi Runnable-like cooking was compiled.
public class ShanZhiRunnable implements Runnable{ Exchanger<String> exchanger; Random random = new Random(); public ShanZhiRunnable(Exchanger<String> exchanger) { this.exchanger = exchanger; } @Override public void run() { while (true){ try { String food = Food.getRandomFood(); System.out.println("==>Yamaguchi started " + food); Thread.sleep(random.nextInt(500)); System.out.println("==>Shanzhiba " + food + " Well done.,Fly to the road"); String exchange = exchanger.exchange(food); System.out.println("==>Yamaghi received comments from Lufei:" + exchange); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Then write LuFei Runnable-like Lufei for Lufei's meal.
public class LuFeiRunnable implements Runnable{ Exchanger<String> exchanger; Random random = new Random(); public LuFeiRunnable(Exchanger<String> exchanger) { this.exchanger = exchanger; } @Override public void run() { String temp = "Eat now..."; while (true){ try { String food = exchanger.exchange(temp); System.out.println("--->Lufei gets Shanzhi's cooking: " + food); Thread.sleep(random.nextInt(500)); System.out.println("--->Lufei finished eating" + food); temp = food + "It's delicious.!Thank you so much for Yamaguchi...."; } catch (InterruptedException e) { e.printStackTrace(); } } } }
Finally, test class TestMain is written.
public class TestMain { public static void main(String[] args) { Exchanger<String> exchanger = new Exchanger<>(); new Thread(new LuFeiRunnable(exchanger)).start(); new Thread(new ShanZhiRunnable(exchanger)).start(); } }
The results are as follows, and some of the output is intercepted.
==> Yamaghi began to make a dish of abalone ==> Shanzhi finished a pint of abalone and sent it to Lufei. ==> Shanzhi received Lufei's comment: Let's eat. ==> Yamaghi started making sesame shredded pork --> Lufei got Shanzhi's dish: a dish of abalone ==> Yamaghi made the shredded sesame meat and sent it to Lufei. --> Lufei finished a dish of abalone --> Lufei got Shanzhi's dish: shredded sesame meat ==> Shanzhi received Lufei's comment: A pint of abalone is delicious! Thank Shanzhi so much. ==> Yamaguchi started making fried tomatoes with eggs --> Lufei finished eating sesame shredded pork ==> Shanzhi cooked the fried eggs and tomatoes and sent them to Lufei. ==> Shanzhi received Lu Fei's comment: Sesame shredded meat is delicious! Thank you so much for Shanzhi. --> Lufei got Shanzhi's dish: fried tomatoes with eggs ==> Yamaghi began to make fried tofu with meat. ==> Shanzhi finished the meat-inlaid bean curd and sent it to Lufei. --> Lufei finished his eggs and fried tomatoes --> Lufei got Shanzhi's dishes: oil tofu with meat ==> Shanzhi received Lufei's comment: Fried tomatoes with eggs are delicious! Thank Shanzhi so much. ==> Yamaguchi began to make steamed fish tail with plum vegetable ==> Shanzhi made steamed fish tail of plum vegetable and sent it to Lufei. --> Lufei finished eating fried tofu with meat ==> Shanzhi received Lufei's comment: Oil tofu with meat is delicious! Thank Shanzhi so much. ==> Shanzhi started making fried chicken --> Lufei got Shanzhi's dish: steamed fish tail with plum vegetable ==> Shanzhi finished the fried chicken and sent it to Lufei. --> Lufei steamed fish tail after eating plum vegetable --> Lufei got Shanzhi's dish: fried chicken ==> Shanzhi received Lufei's comment: Steamed fish tail with plum vegetable is delicious! Thank Shanzhi so much. ==> Yamaghi started making steamed fish with bamboo powder ==> Shanzhi cooked the steamed fish with emerald bamboo powder and sent it to Lu Fei. --> Lufei finished eating fried chicken --> Lufei got Shanzhi's dish: steamed fish with bamboo powder ==> Shanzhi received Lufei's comment: Fried chicken is delicious! Thank Shanzhi so much. ==> Shanzhi began to make amorous goats and willows ==> Shanzhi has made the amorous willow well and sent it to Lufei.
Example 2: impatient Yamaguchi and Luffy
Now suppose they are both impatient and don't want to wait all the time.
Modify LuFei Runnable to read:
public class LuFeiRunnable implements Runnable{ Exchanger<String> exchanger; Random random = new Random(); public LuFeiRunnable(Exchanger<String> exchanger) { this.exchanger = exchanger; } @Override public void run() { String temp = "Eat now..."; while (true){ try { String food = exchanger.exchange(temp, 300, TimeUnit.MILLISECONDS); System.out.println("--->Lufei finished eating" + food); temp = food + "It's delicious.!Thank you so much for Yamaguchi...."; Thread.sleep(random.nextInt(500)); } catch (InterruptedException e) { e.printStackTrace(); } catch (TimeoutException e) { System.out.println("--->Lufei waited impatiently,Don't want to wait......Begin to drink Northeast Wind" ); } } } }
Modify ShanZhiRunnable to
public class ShanZhiRunnable implements Runnable{ Exchanger<String> exchanger; Random random = new Random(); public ShanZhiRunnable(Exchanger<String> exchanger) { this.exchanger = exchanger; } @Override public void run() { while (true){ String food = Food.getRandomFood(); try { System.out.println("==>Yamaguchi started " + food); Thread.sleep(random.nextInt(500)); System.out.println("==>Shanzhiba " + food + " Well done.,Fly to the road"); String exchange = exchanger.exchange(food, 300, TimeUnit.MILLISECONDS); System.out.println("==>Yamaghi received comments from Lufei:" + exchange); } catch (InterruptedException e) { e.printStackTrace(); } catch (TimeoutException e) { System.out.println("==>Yamaguchi's impatience,Don't want to wait......,hold " + food + " The other crew ate it."); } } } }
Run the program:
==> Yamaguchi began to make spicy diced meat ---> Lufei waited impatiently, did not want to wait... began to drink the Northeast Wind. ==> Shanzhi has cooked the spicy diced meat and sent it to Lufei. ==> Shanzhi received Lufei's comment: Let's eat. ==> Shanzhi began to make casserole sauce --> Lufei finishes eating Diced Pork with hot pepper ==> Shanzhi has finished the sandpot and three flavors and sent them to Lufei. ==> Shanzhi received Lu Fei's comment: Spicy meat is so delicious! Thank Shanzhi so much. --> Lufei finished the casserole three flavors ==> Shanzhi began to make shredded pork with sweet pepper ==> Shanzhi finished the shredded sweet pepper meat and sent it to Lufei. --> Lufei finished eating shredded pork with sweet pepper ==> Shanzhi received Lufei's comment: Sandpot is delicious! Thank you so much for Shanzhi. ==> Yamaghi began to make a dish of abalone ==> Shanzhi finished a pint of abalone and sent it to Lufei. ==> Yamaguchi was impatient to wait. He didn't want to wait. He ate a pint of abalone and other crew members. ==> Shanzhi started making crispy chicken legs ---> Lufei waited impatiently, did not want to wait... began to drink the Northeast Wind. ==> Shanzhi finished the crispy chicken legs and sent them to Lufei. ==> Shanzhi received Lufei's comment: Sweet pepper and shredded pork are delicious! Thank Shanzhi so much. --> Lufei finished eating crispy chicken legs ==> Yamaguchi began to make Snow Flake Soup ==> Shanzhi made Snow Flake Soup and sent it to Lufei. --> Lufei finished Snow Flake Soup ==> Shanzhi received Lufei's comment: crispy chicken legs are delicious! Thank Shanzhi so much. ==> Shanzhi began to make anchovy goldfish ==> Shanzhi has finished the anchovy goldfish and sent it to Lufei. ==> Shanzhi received Lufei's comment: Snow flake soup is delicious! Thank Shanzhi so much. --> Lufei finished eating anchovy goldfish ==> Yamaguchi began to make mushroom soaked fish clouds ---> Lufei waited impatiently, did not want to wait... began to drink the Northeast Wind. ==> Shanzhi made a good job of soaking mushrooms in fish clouds and sent them to Lu Fei. ==> Shanzhi received Lufei's comment: anchovies are delicious! Thank Shanzhi so much. --> Lufei finished eating mushroom soaked fish clouds ==> Shanzhi began to make spicy chicken ==> Shanzhi has cooked the spicy chicken and sent it to Lufei. ==> Shanzhi received Lu Fei's comment: Mushroom soaking fish cloud is delicious! Thank Shanzhi so much (?) --> Lufei finished eating spicy chicken ==> Shanzhi began to cook dried plum vegetables and roast meat
Through the above two examples, there should be no difficulty in mastering Exchanger. Okay, that's all for this article.