Win a copy of Emmy in the Key of Code this week in the General Computing forum!

Avi Sarma

+ Follow
since Jun 10, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Avi Sarma

Hi Experts,

I have a question about Java application architecture/design.

I have never done such thing before so wondering if any of you guys have done or seen such design.

Brief application description:
  • 1. There is live Java application hosted on a Web server that provides ticket information. For example: Provide airline tickets to customers
  • 2. This java application provides other functionalities too such as rescheduling of tickets etc.

  • Requirement:
  • 1. What is the best way of making changes while the system is running without customers getting affected? Something like switching the requests from one server to another which has the latest functionalities and improvements including the old server code base.
  • 2. How do you make refinements and improvements whilst the system is still running?

  • Is there any whitepaper or article or blog posts where such things are done before? Or if anyone has done such projects before, their input will be valuable as well!

    Any pointers will be appreciated.

    1 week ago
    Probably, I should try to explain with a scenario

    Lets say there is a bus with 20 tickets available.

    There is a list of 5 people.

    There are two threads named Avi and Campbell.

    Avi starts first and then Campbell.

    My understanding:

    Avi checks the number of tickets in the static variable. Oh, we got 20 tickets and 20 >= 5 for sure, so i should book the first person in the list.
    I go into the loop, take the first person, puts in the register saying hey you got 5 tickets. congrats. booked by Avi. As method is synchronized, Campbell is still outside the method
    waiting for Avi to get out. Once i booked the person-0, i come out of the door,  put the key in the nearby key holder.

    Campbell gets the key and gets inside the method and checks the static variable. Aha, 15 more tickets, I should book a person. You go in, takes the name from the list, not sure
    how you will get to Person-1, still a mystery for me , and then book him. You then reduce the number of tickets to 15-5 = 10, comes out of the method, keep the key in the key holder.

    That is what I want to do. It seems my code is definitely not doing that. I am not able to think of a design to do that.

    Any advice will be appreciated.

    Why static variable is a bad idea? Not sure though. But i checked that When Avi goes out of method after putting Person-0, Campbell still sees Person-0 and so it puts the Person-0 in list again.

    Probably, i am not designing the threads properly.
    1. Where does the ≥ come from? It doesn't say anything about ≥ in your originall post.
    >>Yes. that is just to explain the scenario. sorry if that confused you.
    numOfTickets is not an object. yes. it is a variable which is not thread safe so made it static. this variable has the total number of tickets. initially it is 20.

    2. And why is that field marked static?
    >> the field is marked static so that there is only 1 copy for all threads to work on. this is initially 20. when a thread books a person, it reduces this static variable by 5.

    3. Have you identified which objects are and which aren't thread‑safe?
    >> object on which thread acts is BookingTask. So, i identify it as not thread safe. it has a method which is acted on by all threads. that method is not thread safe as well. so made it syncrhonized.

    hope this helps and makes the situation a little bit clear

    Campbell Ritchie wrote:Welcome to the Ranch

    I am afraid I would reject that code as soon as I saw a parameter called dot which hasn't anything to do with dots, or underscores in field_names.
    I think you need a book about threading; most people don't use new Thread(...), but create a Thread Pool.
    Please identofy which objects in your code aren't thread‑safe, for the first stage of the process.
    Please explain what line 4 in the booking task class means.


    Thanks for the quick reply as well.

    Changed dot to something meaningful. Let me know what you think. New to java I will be more careful

    >> The objects which should be safe are:
    1. numOfTickets

    My idea:
    1. As I mentioned, thread should check the variable first. If number >=5, it should book tickets otherwise not.

    Problem: Not sure, how to make the looping thread safe. I mean, if person 0 is booked by Thread-0, person-0 should not be booked by Thread-1...etc. Possible?

    >> Line 4: It is the total number of tickets. So, my intention was if a thread changes it, it should be available to all the other threads. Tried volatile. But it didn't work. No idea why.

    Hi Everyone,

    I am new to Java multi-threading world. I have read "Head First Java" and other resources but still not clear about a few things like how to design a solution of a problem. For example: I went for an interview and they asked me this question:
    Problem: Write code for a bus ticket reservation system.
    1. Number of tickets available: 20
    2. Each person will be able to reserve 5 tickets or none at a time.
    3. Multiple threads will try to reserve the tickets
    4. Print the final booking information

    My code which is a mess. Can anyone help me out in making it better? Output is a mess. Threads doesn't synchronize. Same person is booked twice. Please help me design it in a better way.