简体   繁体   English

Java Runnable多线程在线程之间共享中心对象

[英]Java Runnable Multi threading Sharing Central Object between threads

I am searching for a simple Java Runnable example where 3 individual threads individually add a item to a hash table available to all threads and display on join of thread's completion. 我正在寻找一个简单的Java Runnable示例,其中3个单独的线程分别向所有线程可用的哈希表中添加一个项目,并在线程完成的连接处显示。

Simple example: 简单的例子:

Hashtable 哈希表

Thread 1: Add to Hashtable Key1 Keyvalue1 线程1:添加到哈希表键1键值1

Thread 2: Add to Hashtable Key2 Keyvalue2 线程2:添加到哈希表Key2 Keyvalue2

Thread 3: Add to Hashtable Key3 Keyvalue3 线程3:添加到哈希表Key3 Keyvalue3

on join print hashtable 在联接上打印哈希表

Any help would be much appreciated, 任何帮助将非常感激,

Thank you 谢谢

So what you want is a single HashTable which is edited by multiple Threads? 那么,您想要一个由多个线程编辑的单个HashTable吗? There shall be 3 Threads and everyone shall put a diffent key and a different value into the Table and after every put the active thread shall print out all keys and values in pairs? 应该有3个线程,每个人都应将一个不同的键和一个不同的值放入表中,并且在每次放置后,活动线程应成对打印所有键和值吗? Am i right? 我对吗? Because if, this might be something: 因为如果这样,可能是这样的:

import java.util.Hashtable;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

public class Threadexample {

    // Hashtable and Keylist
    private Hashtable<String, Integer> table;
    private CopyOnWriteArrayList<String> keys;

    // Konstruktor
    public Threadexample() {
        table = new Hashtable<String, Integer>();
        keys = new CopyOnWriteArrayList<String>();
    }

    // Adds an Item to the Table and prints the table
    public synchronized void addItem(String key, int value, String threadname) {
        // Adding
        table.put(key, value);
        if (!isAlreadyAdded(key)) {
            keys.add(key);
        }else{
        return;
        }
        System.out.println(threadname + "-> Added! Key: " + key + " Value: " + value);
        // Showing
        showItems(threadname);
    }

    // Bewares of doublicate keys in the keylist
    private boolean isAlreadyAdded(String key) {
        int size = keys.size();
        for (int i = 0; i < size; i++) {
            if (keys.get(i).equals(key)) {
                return true;
            }
        }
        return false;
    }

    // Prints out all Integer with their keys
    private void showItems(String threadname) {
        Set<String> keys = table.keySet();
        for (String key : keys) {
            System.out.println(threadname + "-> Key: " + key + "   Value: " + table.get(key));
        }
        System.out.print(System.getProperty("line.separator"));
    }

    // Mainmethod
    public static void main(String[] args) {
        final Threadexample tex = new Threadexample();
        final String[] keyarray = new String[] { "Zero", "One", "Two" };

        // starts 3 Threads which are adding and showing the Objects
        for (int i = 0; i < 3; i++) {
            final int value = i;

            new Thread() {
                public void run() {
                    setName("Thread: " + (value + 1));
                    tex.addItem(keyarray[value], value, getName());
                }
            }.start();

            try {
                // Leave every Thread enough time to work
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        }

    }
}

You will need to use semaphore for synchronation. 您将需要使用信号量进行同步。 Here is an exemple : http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html 这是一个示例: http : //docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html

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

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