线程的执行没有给出正确的结果

问题描述 投票:0回答:1

我在那里使用了java类,并且在每个类中我都使用了重写run方法。

以下是课程:

package multithreading.marriageexp;

import java.util.Scanner;

public class Distribution implements Runnable{

    private int totalPersons;

    public void setTotalPersons(int totalPersons) {
        this.totalPersons = totalPersons;
    }

    @Override
    public void run(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of guests:: ");
        totalPersons = sc.nextInt();
    }

    public int getTotalPersons(){
        return totalPersons;
    }
}
package multithreading.marriageexp;

import java.util.ArrayList;
import java.util.List;

public class Venue implements Runnable{

    List<String> venue = new ArrayList<>();

    public List<String> getVenue() {
        return venue;
    }

    public void setVenue(List<String> venue) {
        this.venue = venue;
    }

    @Override
    public void run(){
        venue.add("India");
        venue.add("Canada");
        venue.add("Australia");
    }
    
}

package multithreading.marriageexp;

import java.util.List;

public class WeddingCard implements Runnable{

    @Override
    public void run(){
        System.out.println("Below is the details for the Weddings: ");

        System.out.println("Planned Venues to be picked from::");
        Venue v = new Venue();
        List<String> venuesList =  v.getVenue();

        venuesList.forEach(x -> System.out.print(x + " "));

        Distribution d = new Distribution();

        System.out.println("And Total guests:: "+ d.getTotalPersons());

    }
}

class WeedingResult{
    public static void main(String[] args) throws InterruptedException{

        Venue venue = new Venue();
        Thread t1 = new Thread(venue);

        Distribution distribution = new Distribution();
        Thread t2 = new Thread(distribution);

        t1.start();
        t1.join();

        t2.start();
        t2.join();


        WeddingCard weddingCard = new WeddingCard();
        Thread t3 = new Thread(weddingCard);

        t3.start();
        t3.join();

        System.out.println("--------------End of main Thread.--------------------");


    }
}

这里我没有得到场地列表的结果,也没有从分布类中得到结果,即 d.getTotalPersons():

这是我通过输入 7 作为客人总数得到的结果:

输入客人人数:: 3 以下为婚礼详情: 计划场地选自:: 金奈孟买浦那--------------主线程结束。--------------------

我尝试解决它,但没有得到它的行为原因。

java list multithreading operating-system
1个回答
0
投票

错误原因:

我发现在 WeddingResult 中,最初在主函数中创建了一个实例,然后将其传递给 Thread() - 这是接收输入的地方。之后,在 WeddingCard 类的 run() 方法中,创建另一个实例,该实例不会调用任何内容,只有实例存在,但没有为实例变量(即,总人数和场地)分配值。因此我们没有得到任何结果。

解决方案:

我们可以在 t3 线程的 run() 方法中启动 t1 和 t2 线程,这样首先 t3 启动,然后 t1、t2 加入执行并结束。此外,我们可以直接传递我们创建的唯一一个实例来获取输出,而不是创建另一个实例。

这是修改后的代码: (我假设该文件的名称为 WeedingResult.java)

package multithreading.marriageexp;

import java.util.List;

class WeddingCard implements Runnable {

    @Override
    public void run() {

        Venue venue = new Venue();
        Thread t1 = new Thread(venue);

        Distribution distribution = new Distribution();
        Thread t2 = new Thread(distribution);

        t1.start();
        try {t1.join();} catch (InterruptedException e) {e.printStackTrace();}

        t2.start();
        try {t2.join();} catch (InterruptedException e) {e.printStackTrace();}

        System.out.println("Below is the details for the Weddings: ");

        System.out.println("Planned Venues to be picked from::");

        List<String> venuesList = venue.getVenue();

        venuesList.forEach(x -> System.out.print(x + " "));

        System.out.println("And Total guests:: " + distribution.getTotalPersons());

    }
}

class WeedingResult {
    public static void main(String[] args) throws InterruptedException {

        WeddingCard weddingCard = new WeddingCard();
        Thread t3 = new Thread(weddingCard);

        t3.start();
        t3.join();

        System.out.println("--------------End of main Thread.--------------------");

    }
}

顺便说一句,我是 java 的新手 - 因此,如果 解决方案不对。 干杯!!

© www.soinside.com 2019 - 2024. All rights reserved.