Antwan
Antwan

Reputation: 3876

How to make two threads work in same time

I make two threads: one for fill an array and the second one print it. It seems like the two thread don't work in same time.

When i run the code its print first thread working then its print the array and second thread working how i can know if they working on same time or not?

here is my code:

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        firstthread f=new firstthread();            
        secondthread s=new secondthread();

        s.start();
        f.start();         
    }

}

and the class the contain both fill and print method:

public class Array {
private static int [] ar=new int[500];

 public static void fillarray()
 {
     for (int i = 0; i < ar.length; i++)         
            ar[i]=i;
 }

 public static void printarray()
 {
     for (int i = 0; i < ar.length; i++)         
          System.out.print(ar[i]+" ");  
 }
 }

the first thread class:

    public class firstthread extends Thread{

            public void  run() {

                   Array.fillarray();
                System.out.print("first thread working ");

            }

    }

the second thread class:

    public class secondthread extends Thread{


        public void run() {

            Array.printarray();

            System.out.println("second array working");
        }

    }

Upvotes: 0

Views: 995

Answers (4)

TheLostMind
TheLostMind

Reputation: 36304

You want an implementation of producer-consumer problem bro... Frankly speaking, without having a lock on the Object's monitor, you dont have control over anything. You dont have control over how much time a thread executes (Timeslice provided to it). If the time slice is too long, you will feel that one thread executes after another because your processor is so fast, your first thread will get ample time to finish its work . If you want to know how threads work exactly, use wait() and notify() and synchronized methods to change it into a producer-consumer problem.

  1. Jeanne is also right, you can put sleep() , there's a 90 percent chance that it might work.
  2. You could increase the number of elements being filled/printed.
  3. There's a 70% chance of you reading more than what you wrote(filled in the array) if you dont use syncronized/wait/notify.

Upvotes: 1

Rhycce
Rhycce

Reputation: 111

Question isn't clear. Do you mean how can you tell if they are working on the same array? If that is the case the answer is Yes. the array is static meaning there would be only one of its kind which would belong to the class array. So there wont be multiple instances of it being worked on by the different threads.

As stated above, the threads run very fast. So even though they are accessing the same object, one thread would finish its job before the second even begins

Upvotes: 0

user3001267
user3001267

Reputation: 304

Starting a thread takes some time to the JVM. The first thread executes its run() method faster than the other thread is started. That's why you see that behavior.

Upvotes: 1

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

The fillarray and printarray methods are running too fast so you aren't getting threading.

Add Thread.sleep(10) to each loop. That way the code will run much slower and the threads will intersperse. They will probably alternate with this approach.

Then change in to sleep a random # of seconds and you'll see different behavior.

Upvotes: 2

Related Questions