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

  • Home
  • SEARCH
  • 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 8938823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:43:12+00:00 2026-06-15T10:43:12+00:00

This is a tiny implementation of parking lots that I found in a book.

  • 0

This is a tiny implementation of parking lots that I found in a book.
I’m wondering how to end a java thread.
Would like to stop it if there is still no free parking lots after a certain time passed, in my case sleep(500).

import java.util.logging.Level;
import java.util.logging.Logger;

class ParkingLot {

// I assume that parking lot capacity is 4 lots
public static final int MAX_CAPACITY = 4;
private int totalParkedCars = 0;
public synchronized void park(String vehicle) {

    // check if there is a free lot
    while (totalParkedCars >= MAX_CAPACITY) {
        try {
            System.out.println("The parking lot is full " +
                    vehicle + " has to wait.");

            // a time that vehicle will wait for a parking lot to free
            wait(500);
 // ! I wait and if no free lots want to stop thread !
        } catch (InterruptedException e) {
                       Logger.getLogger(ParkingLot.class.getName()).log(Level.SEVERE, 
                    "Sorry for inconvenience :(", e);
        }

    }
    // precondition is true
    System.out.println(vehicle + " has parked");
    totalParkedCars++;
}
public synchronized void leave(String vehicle) {
    totalParkedCars--;
    System.out.println(vehicle + " has left, notify a waiting vehicle.");
    notify();
}
} 

Vehicle class:

import java.util.logging.Level;
import java.util.logging.Logger;

class Vehicle extends Thread {
private ParkingLot parking;
private String name;
private volatile boolean running = true;
public Vehicle(String name, ParkingLot parking) {
    this.name = name;
    this.parking = parking;
    start();
}
public void terminate() {
    running = false;
}
public void run() {
    while (running) {
        System.out.println(this.name + " is ready to park.");
        parking.park(this.name);
        try {
            // Vehicle waits at lot
            sleep(1000);  // TODO: random time to wait                  
        } catch (InterruptedException e) {
            // Will catch and log InterruptedException if happens
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, this.name, e);
            running = false;
        }

        // Vehicle leaves parking lot
        parking.leave(this.name);   
        running = false;
    }
}
 }

A class for testing:

public class GSTest {
public static void main(String[] args) {
    ParkingLot parkingLot = new ParkingLot();
    new Vehicle("Vehicle_1", parkingLot);
    new Vehicle("Vehicle_2", parkingLot);
    new Vehicle("Vehicle_3", parkingLot);
    new Vehicle("Vehicle_4", parkingLot);
    new Vehicle("Vehicle_5", parkingLot);
    new Vehicle("Vehicle_6", parkingLot);
}
}
  • 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-15T10:43:13+00:00Added an answer on June 15, 2026 at 10:43 am

    Advice can be given on several levels:

    1. Use the Java interruption mechanism: call yourThread.interrupt() and within the thread you’ll get an InterruptedException when you enter sleep (or while you are already sleeping);

    2. don’t stop your thread from the outside; use a condition inside the thread;

    3. and this would be the best option: don’t use an endless loop with sleep; instead schedule a repeated task that you can cancel when you need to.

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

Sidebar

Related Questions

I'm trying to create tiny urls like this: site.com/abc123 goes to: site.com/index.php?token=abc123 but I
this is my first question in here, and I would like to ask if
I have this tiny Qt project with a project file like this: TEMPLATE =
I wrote this tiny little wrapper around order , but I fear my implementation
I'm making this tiny utility program (Windows Forms) and it would need to save
OK, so I have this tiny little corner of my code where I'd like
I am using the tiny scrollbar plugin and implementation of this example . All
There's something odd about the implementation of the BoundedExecutor in the book Java Concurrency
Please could someone help diagnose this tiny problem that shouldn't be one - trying
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =

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.