Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- class Main {
- public static void main(String[] args) throws Exception {
- System.out.println("Hello world!");
- List<Apple> apples = Collections.synchronizedList(new ArrayList<>());
- Thread t1 = new Thread(new Farmer(apples));
- t1.start();
- Thread t2 = new Thread(new Farmer(apples));
- t2.start();
- while (apples.size() == 0) {
- // System.out.println(apples.size());
- }
- System.out.println(apples.size() + " main ended");
- }
- }
- ________________________________________________________________________________
- import java.util.*;
- public class Farmer implements Runnable {
- List<Apple> busket;
- private static int count;
- private int id;
- private int appleCount;
- public Farmer(List<Apple> busket) {
- this.busket = busket;
- id = count++;
- }
- public void makeApple() {
- Apple apple = new Apple("Apple" + appleCount + " by Farmer" + id);
- System.out.println("making apple: " + apple);
- busket.add(apple);
- appleCount++;
- }
- public void run() {
- while (appleCount < 5) {
- makeApple();
- }
- }
- }
- ________________________________________________________________________
- public class Apple {
- private String name;
- public String getName() {
- return name;
- }
- public String toString() {
- return name;
- }
- public Apple(String name) {
- this.name = name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement