Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法按照Java中的要求实现线程_Java_Multithreading_Parallel Processing_Synchronization_Thread Safety - Fatal编程技术网

无法按照Java中的要求实现线程

无法按照Java中的要求实现线程,java,multithreading,parallel-processing,synchronization,thread-safety,Java,Multithreading,Parallel Processing,Synchronization,Thread Safety,我在实现以下场景时遇到困难,因为我是多线程新手。有人能帮我吗 要求如下: 一个线程创建了从1到无穷大的素数。另一个将这些数字写入控制台的线程。编写运行这两个线程的代码。 这就是我所尝试的: class func1 implements Runnable{ public void run(){ int x=1, counter=0; for (int i=0; i<=infinity; i++){ if (isPrime(i)){

我在实现以下场景时遇到困难,因为我是多线程新手。有人能帮我吗

要求如下:

一个线程创建了从1到无穷大的素数。另一个将这些数字写入控制台的线程。编写运行这两个线程的代码。

这就是我所尝试的:

class func1 implements Runnable{



    public void run(){
    int x=1, counter=0;
    for (int i=0; i<=infinity; i++){
        if (isPrime(i)){
            p[counter] = i;
            counter++;
        }
    }
    }
}

class func2 implements Runnable{

    public void run(){
    int x=1, counter=0;
    int[] p=0;
    for (int i=0; i<=infinity; i++){
                System.out.println(“Prime number:”+p[i]);       

    }
    }
}
public class test{
 static volatile int[] arr;
public static void main(String[] args){
    func1 obj1 = new func1(arr);
    func2 obj2 = new func2(arr);
    thread t1, t2;
    t1 = new thread(obj1);
    t2 = new thread(obj2);
    t1.start();
    t2.start();
}
类func1实现可运行{
公开募捐{
int x=1,计数器=0;

对于(int i=0;i这里是生产者-消费者模式的一个简单示例。生产者创建一些整数值,将它们填充到阻塞队列中,消费者从队列中取出它们

制作人:

import java.util.concurrent.BlockingQueue;

public class Producer implements Runnable{
    BlockingQueue<Integer> queue;

    public Producer(BlockingQueue<Integer> queue){
        this.queue = queue;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            queue.offer(i);
        }
    }
}

您可以生成/使用素数。生产者/消费者的run()方法中的逻辑将发生更改。这也可以使用wait/notify来实现。

听起来像是您正在尝试为其实现生产者/消费者模式
(int i=0;iinfinity只是随机的。请忽略。您可以在不使用阻塞队列的情况下实现此功能吗?生产者/消费者可以在不使用阻塞队列的情况下实现。这就是我要找的。您可以在这里回答吗?
import java.util.concurrent.BlockingQueue;

public class Consumer implements Runnable {
    BlockingQueue<Integer> queue;

    public Consumer(BlockingQueue<Integer> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            if (queue.peek() != null) {
                Integer number = queue.poll();
                System.out.println("Integer is >>> " + number);
            }
        }
    }
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;


public class Main {
    public static void main(String args[]) {
        BlockingQueue queue = new ArrayBlockingQueue(10);
        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        Thread producerThread = new Thread(producer);
        Thread consumerThread = new Thread(consumer);

        producerThread.start();
        consumerThread.start();
    }
}