Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:11:20+00:00 2026-06-04T11:11:20+00:00

I try to make a simple program that does the following: I have two

  • 0

I try to make a simple program that does the following:

I have two cranes that take containers off a ship. They put the containers on the quay, where 2/3 trucks pick them up. Cranes can put a maximum of 5 containers on the quay.

The problem is the following: when I don’t have breakpoints, the crane threads just pick containers of the ship and put them onto the quay and then the program stops. This is the output every time:

Kraan 2 heeft container nummer 1 van het schip gehaald.
Kraan 2 heeft container nummer 1 op de kade geplaatst.
Kraan 1 heeft container nummer 2 van het schip gehaald.
Kraan 1 heeft container nummer 2 op de kade geplaatst.
Kraan 2 heeft container nummer 3 van het schip gehaald.
Kraan 1 heeft container nummer 4 van het schip gehaald.
Kraan 2 heeft container nummer 3 op de kade geplaatst.
Kraan 2 heeft container nummer 5 van het schip gehaald.
Kraan 2 heeft container nummer 5 op de kade geplaatst.
Kraan 1 heeft container nummer 4 op de kade geplaatst.
Kraan 2 heeft container nummer 6 van het schip gehaald.
Kraan 1 heeft container nummer 7 van het schip gehaald.

So, the truck threads do not run. When I put a breakpoint in Wagen.VoerWerkzaamhedenUit(), the breakpoint does get hit and miraculously the whole program runs till all containers have been processed.

Here’s my code:

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                Schip.LaadContainerOpSchip(i);
            }

            Thread kraan1 = new Thread(new Kraan().VoerWerkzaamhedenUit);
            kraan1.Name = "Kraan 1";
            kraan1.Start();

            Thread kraan2 = new Thread(new Kraan().VoerWerkzaamhedenUit);
            kraan2.Name = "Kraan 2";
            kraan2.Start();

            Thread wagen1 = new Thread(new Wagen().VoerWerkzaamhedenUit);
            wagen1.Name = "Wagen 1";
            wagen1.Start();

            Thread wagen2 = new Thread(new Wagen().VoerWerkzaamhedenUit);
            wagen2.Name = "Wagen 2";
            wagen2.Start();

            kraan1.Join();
            kraan2.Join();
            wagen1.Join();
            wagen2.Join();

            Console.WriteLine("Press any key to continue...");
            Console.Read();
        }
    }

public class Kraan
    {
        private Random random = new Random();

        public void VoerWerkzaamhedenUit()
        {
            while (Schip.HeeftContainers())
            {
                Thread.Sleep(random.Next(1000, 6000));

                int container = Schip.VerwijderContainer();

                if (container != -1)
                {
                    Console.WriteLine("{0} heeft container nummer {1} van het schip gehaald.", Thread.CurrentThread.Name, container);

                    Thread.Sleep(random.Next(1000, 6000));

                    Kade.PlaatsContainer(container);

                    Console.WriteLine("{0} heeft container nummer {1} op de kade geplaatst.", Thread.CurrentThread.Name, container);
                }
            }
        }
    }

public static class Schip
    {
        private static List<int> containers = new List<int>();

        public static void LaadContainerOpSchip(int container)
        {
            containers.Add(container);
        }

        public static int VerwijderContainer()
        {
            lock (containers)
            {
                int container = -1;

                if (containers.Any())
                {
                    container = containers[0];

                    containers.RemoveAt(0);
                }

                return container;
            }
        }

        public static bool HeeftContainers()
        {
            lock (containers)
            {
                return containers.Any();
            }
        }
    }



public static class Kade
    {
        private static List<int> containers = new List<int>();

        public static void PlaatsContainer(int container)
        {
            lock (containers)
            {
                while (containers.Count == 5)
                {
                    Monitor.Wait(containers);
                }

                containers.Add(container);

                Monitor.PulseAll(containers);
            }
        }

        public static int VerwijderContainer()
        {    
            lock (containers)
            {
                while (containers.Count == 0)
                {
                    Monitor.Wait(containers);
                }

                int container = -1;

                if (containers.Any())
                {
                    container = containers[0];

                    containers.RemoveAt(0);
                }

                Monitor.PulseAll(containers);

                return container;
            }
        }

        public static bool HeeftContainers()
        {
            lock (containers)
            {
                return containers.Any();
            }
        }
    }

public class Wagen
    {
        private Random random = new Random();

        public void VoerWerkzaamhedenUit()
        {
            while (Kade.HeeftContainers())
            {
                Thread.Sleep(random.Next(1000, 6000));

                int container = Kade.VerwijderContainer();

                if (container != -1)
                {
                    Console.WriteLine("{0} heeft container nummer {1} van de kade gehaald.", Thread.CurrentThread.Name, container);

                    Thread.Sleep(random.Next(1000, 6000));
                }
            }
        }
    }

And no, I do not want to use a blocking collection, I want to use a List<int> 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:11:22+00:00Added an answer on June 4, 2026 at 11:11 am

    When the program starts, the trucks check the quay (Kade.HeeftContainers()) before the cranes get a chance to put containers on the quay so the truck threads exit right away. Then the cranes continue filling up the quay until it reaches the limit of 5 containers. Since the truck threads already exited, the quay stays full so crane threads stop waiting on Monitor.Wait(containers);.

    To resolve the issue, the trucks need to keep running until all the containers have gone through the pipeline. For example, you can have a counter on the quay (e.g. loadedContainers) and increment this counter everytime a container is removed from the quay to be loaded on the trucks. Then return loadedContainers == 100; in the Kade.HeeftContainers getter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so I'll try to make that question a little more simple. The core
I'm learning java and I have made simple program that simply reads value from
I'm trying to make a simple program that shows that lets an image bob
I wanted to make some simple file recovery software, where I want to try
New to batch files, first try actually. Trying to make a simple batch file
I try to make have an EventListener in ItemRenderer but its not working. How
I try to make a displaying list with onmouseover() with some links but they
I have a program that periodically polls a certain folder for log files. When
I am trying to create a simple program that reads a string of 4
I'm trying to make a very simple 'counter' that is supposed to keep track

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.