Hi everyone,
I need some help with my homework. I checked online and found loads of results but didnt understand much.
I need to submit hw by tomm nyt .. 27hrs from now. Please help me if you can plz ..

Here is my code snippet

public static void main(String[] args) 
    {
        // TODO Auto-generated method stub

        initilize();        

        System.out.println("Barbershop Opened with "+config.getNoofbarbers()+" barbers.\nWaiting for Customers ... ");              // Starting Barber Shop

        System.out.println("Enter the no of minutes :");
        config.setNoofminutes(Integer.parseInt(input.nextLine()));

        Customer_Enters thread_ce = new Customer_Enters(config);
        Barber_Working thread_bw1 = new Barber_Working(config, 0);
        Barber_Working thread_bw2 = new Barber_Working(config, 1);
        Barber_Working thread_bw3 = new Barber_Working(config, 2);
        new Thread(thread_ce).start();
        new Thread(thread_bw1).start();
        new Thread(thread_bw2).start();
        new Thread(thread_bw3).start(); 

    }

I basically have 4 threads running.
Now i need to make sure in the parent class that they run only for a unser-provided specific amount of time.

I need to put some sort of timer i guess .. I have never done it .. any help will be highly appretiated.:))

Thanks a lott in advance ...

If you need any more information .. plz lemme know .. i ll be more dan happy to help .. :)
Thanks agn...

Each thread could get the time it started by calling the System class's currentTimeMillis() method when it starts and call it later in execution and compute the duration the thread has run by subtracting the start value from the current value . When the user time has run out, the thread would exit.

@NormR1 how will my main function know when the time os over.
I need to do some furthure execution in main so i need to know here when to kill threads and go ahead ...

okk I updated the code a lill bit ...

here it is ...

Thread.sleep(config.getNoofminutes());

        new Thread(thread_ce).stop();
        new Thread(thread_bw1).stop();
        new Thread(thread_bw2).stop();
        new Thread(thread_bw3).stop();

        //odr stuff ...

my question is ...
First - is that the right way of ending a thread? bcoz its not working ...
all its doing is pausing i think and it starts running as soon as my //odr stuff part is over ...:( ...

Second - this sleep i am calling in main function ... if i sleep my main thread will that also make my spawned threads to sleep ?? Its doing that ...

How do i avoid dat ???

how will my main function know when the time os over.

The thread could call a method before it exits to tell other code that it is exiting the thread.

if i sleep my main thread will that also make my spawned threads

That shouldn't happen. A call to the sleep() only effects the thread that calls sleep(). The other threads will continue executing.

new Thread(thread_bw3).stop();
This code has several problems.
It makes not sense to create a new thread and immmediate call its stop() method.
The stop() method is deprecated and should not be used.

I'm confused about what you are trying to do. Can you explain in more detail?
Is each thread to have its own duration? The posted code makes it look like there is only one duration for all the threads.

@NormR1 ...
I dont know dats what happens with every odr thread ... but when i tried pausing the main thread in a diff test project also - it was paunsing all the chile threads also .. :(

About stopping .. yea i need all the threads to work for only specific duration - same for everything ... after dat duration all threads stop and result is printed out ..
As stop() is a depricated code and not advisable to use - for now i have put timne limit on all the threads individually ... and display thread also waits for that much time and den display results ...
But this isnt a good code .. I only did it so that i can submit in time ..

I need to read more abt thread intractions ...

As you suggested I am maintaning a global variable avail to all the threads ... display reads that as well to make sure all the threads are done processing ...

I have one question that i need to know for future ..
When i am running the 4 threads there is a time lag ... and I wanted to be able to make all 4 threads work parallelly .. now I know it depends on OS and threads cant be controlled without signalling .. But is there any odr way to make sure all the threads run at exact same moment ..

Ryt now i m good bcoz the interval they run for is much larger dan the minute time lag .. but i wanted to make it perfect for my satisfaction .. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.