简体   繁体   English

如何使两个线程同时工作

[英]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: 并且该类同时包含fill和print方法:

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");
        }

    }

The fillarray and printarray methods are running too fast so you aren't getting threading. fillarray和printarray方法运行速度太快,因此您不会获得线程。

Add Thread.sleep(10) to each loop. 将Thread.sleep(10)添加到每个循环。 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. 然后进入随机睡眠#秒,您将看到不同的行为。

Starting a thread takes some time to the JVM. 启动线程需要花费一些时间到JVM。 The first thread executes its run() method faster than the other thread is started. 第一个线程比另一个线程启动执行它的run()方法更快。 That's why you see that behavior. 这就是为什么您看到这种行为。

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). 您无法控制线程执行的时间(提供给它的Timeslice)。 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. 如果您想知道线程如何正常工作,请使用wait()和notify()以及同步方法将其更改为生产者-消费者问题。

  1. Jeanne is also right, you can put sleep() , there's a 90 percent chance that it might work. 珍妮也是对的,您可以放下sleep(),它有90%的可能会起作用。
  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. 如果您不使用同步/等待/通知,那么您阅读的内容有70%的机会要超过您写的内容(填写在数组中)。

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 因此,即使他们正在访问同一对象,一个线程也将在第二个线程开始之前完成其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM