1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Dmitry_Shevchenko [17]
3 years ago
9

A single-lane bridge connects village A to village B. Farmers in the two villages use this bridge to deliver their products to t

he neighboring town. The bridge can become deadlocked if a farmer from village A and a farmer from village B get on the bridge at the same time. Using semaphores and/or mutex locks (do not be concerned about starvation) implement your solution in Java. In particular, represent village A and village B farmers as separate threads. Once a farmer is on the bridge, the associated thread will sleep for a random period of time, representing traveling across the bridge.
Business
1 answer:
Nookie1986 [14]3 years ago
5 0

Answer:

Answer: Main Java

Explanation:

code:

Main.java

import java.lang.InterruptedException;

import java.lang.Thread;

import java.util.Random;

public class Main {

   final private static int FARMERS = 10;

   public static void main(String[] args) {

       Bridge bridge = new Bridge();

       Random r = new Random();

       System.out.println("Running with " + FARMERS + " farmers...");

       // Enter a bunch of farmers from different directions.

       for (int i = 0; i < FARMERS; i++) {

           Farmer farmer;

           if (r.nextBoolean()) {

               farmer = new SouthBoundFarmer(bridge);

           } else {

               farmer = new NorthBoundFarmer(bridge);

           }

           cross(farmer);

       }

   }

   private static void cross(Farmer f) {

       new Thread(f).start();

   }

}

SouthBoundFarmer.java

public class SouthBoundFarmer extends Farmer {

   public SouthBoundFarmer(Bridge b) {

       super(b);

       this.name = "South";

   }

}

Farmer.java

import java.lang.InterruptedException;

import java.util.Random;

public class Farmer implements Runnable {

   private Bridge bridge;

   private Random random;

   protected String name;

   public Farmer(Bridge b) {

       this.bridge = b;

       this.random = new Random();

   }

   public void crossBridge(Bridge bridge) {

       System.out.println("[" + this.name + "] Waiting to enter bridge...");

       try {

           bridge.enter();

           System.out.println("[" + this.name + "] Entering bridge...");

           // Crossing bridge...some farmers are fast, others are slow :P

           Thread.sleep(1000 + random.nextInt(9000));

           System.out.println("[" + this.name + "] Leaving bridge...");

       } catch (InterruptedException e) {

           System.out.println("...Interrupted!");

       } finally {

           bridge.leave();

       }

   }

   public void run() {

       this.crossBridge(this.bridge);

   }

}

You might be interested in
The basic organizing legal document for a home-rule city is called the
Alex777 [14]
The basic organizing legal document for a home rule city is called the CITY STATUE.
Home rule refers to the government of a city, colony or an independent country by its own citizens. A general law municipality usually become eligible for home rule when the population of the people living there is more than 5,000. The law which govern the activities of those in the home rule city is called statue.
4 0
3 years ago
You work for 48 hours at $8.75 an hour and pay 12% in taxes. what is your net pay biweekly?
Gala2k [10]

I think it is $739.20 for two weeks.

6 0
3 years ago
Read 2 more answers
How can we make sure animal shelters have food and space
Nikitich [7]

Answer:We can donate money or raise a fundraiser to get all the dogs who are in the shelter into a forever home or we can adopt a pet so there will be more space for a new dog to come.

Explanation:

5 0
2 years ago
Suppose People's bank offers to lend you $10,000 for 1 year on a loan contract that calls for you to make interest payments of $
vodka [1.7K]

Answer:

10.38%

Explanation:

The formula to compute the effective annual rate of the loan is shown below:

= (1 + nominal interest rate ÷ periods)^ number of period - 1

The nominal interest rate is shown below:

= $250 × 4 ÷ $10,000

= $1,000 ÷ $10,000

= 0.1

Now the effective annual rate is

= (1 + 0.1 ÷ 4)^4 - 1

= (1 + 0.025)^4 - 1

= 1.025^4 - 1

= 10.38%

Since the interest rate is measured on a quarterly basis, we know there are four quarters in a year and we do the same in the calculation part.

This is the answer but the same is not provided in the given options

4 0
3 years ago
Which of the following is an inconsistency of using market multiples to determine value? A) Using a market multiple assumes that
VikaD [51]

Answer:

B) Using a market multiple assumes that the target company is mispriced, while comparable companies are correctly priced.

Explanation:

Market Multiple, also known as trading multiples, is used to compare two financial measures, to determine the value of a company. It is another name for Price to Earnings Ratio (also called P/E Ratio).

Using the market multiple approach, investors can determine whether stocks in their portfolios will increase or decrease in price through the next term. Investors may then buy or sell stocks in order to maximize their expected gains calculated.

6 0
3 years ago
Other questions:
  • The principle benefit of tariff protection goes to a. foreign consumers of the good produced. b. domestic consumers of the good
    12·1 answer
  • Based on the industry-low, industry-average, and industry-high values for the benchmarked data on p. 7 of the FIR, which one of
    8·1 answer
  • Which of the following is correct with respect to rainy day funds? Multiple Choice Rainy day funds are classified as committed o
    8·1 answer
  • Which of the following is not true about commission-only compensation plans for sales persons? Sales personnel are only compensa
    9·1 answer
  • a black female employee is told that she cannot come to work with her hair in a decorative braids traditionally worn in Africa,
    15·1 answer
  • Rizenton-Pharm, a pharmaceutical company, produces a significant amount of chemical waste every day. The company disposes the wa
    9·1 answer
  • Janet has almost finished painting a neighbor's house, at which time she'll be paid $2,000. The fact that she is increasingly un
    5·1 answer
  • What is the main difference between a market and a command economy?
    7·1 answer
  • The Ryan Corporation uses the composite method and its composite rate is 7.5% per year. The entry that should be made when plant
    6·1 answer
  • What dApp does the author describe as a set of smart contracts that stores data on a home-listing blockchain?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!