I have just started doing threading this week and I'm kind of stuck on one of the exercises.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RandomNumberConsumer implements Runnable {
ArrayBlockingQueue<Integer> numbersProduced;
public RandomNumberConsumer(ArrayBlockingQueue<Integer> numbersProduced) {
this.numbersProduced = numbersProduced;
}
//Should eventually hold the sum of all random number consumed
int sumTotal = 0;
List<Integer> below50 = new ArrayList();
List<Integer> aboveOr50 = new ArrayList();
#Override
public void run() {
//In this exercise, we start four threads, each producing 100 numbers, so we know how much to consume
for (int i = 0; i < 400; i++) {
try {
System.out.println("first" + numbersProduced.take());
System.out.println("second" + numbersProduced.take());
System.out.println("third" + numbersProduced.take());
System.out.println("fourth" + numbersProduced.take());
} catch (InterruptedException ex) {
Logger.getLogger(RandomNumberConsumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public int getSumTotal() {
return sumTotal;
}
public List<Integer> getBelow50() {
return below50;
}
public List<Integer> getAboveOr50() {
return aboveOr50;
}
}
Basically what I don't understand is how to update sumTotal variable to show all consumed random numbers since they're stored in ArrayBlockingQueue<> and also how to insert them into either the below 50 or above Or 50 Lists.
What about don't use numbersProduced.take() in the run method and define a new function, even in RandomNumberConsumer class?
int foo() throws InterruptedException{ //because of take function
int a=numbersProduced.take();
if(a<50)
below50.add(a);
else
over50.add(a);
sumTotal=sumtTotal+a;
return a;
}
now in run call foo and not numbersProduced.take()
I must tell you, i hope you know it: if you use threads which modify the same variable you must use Semaphore or synchronized methods.
Ps: sorry for bad English :)
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am a bit lost...I'm learning Java and have to program a small poll command line application.
We are supposed to program it in german first(to be consistent between us all), so I'll try to translate it, so it's easier to read for you.
My problem is, that it's throwing an exception (while compiling) as following:
Exception in thread "main" java.lang.NullPointerException
at communication.Poll.addQuestionItem(Poll.java:18)
at main.PollTool.main(PollTool.java:8)
am I initializing my array "questionItems" wrong? Aren't I supposed to do it like that? What's wrong here? Did I forget something? :(
main.PollTool:
package main;
import communication.Poll;
public class PollTool {
public static void main(String[] args) {
Poll poll = new Poll ("Best Smartphone:",3);
poll.addQuestionItem("iPhone"); //<--line 8
poll.addQuestionItem("Android");
poll.addQuestionItem("Windows Phone");
poll.askQuestions("This poll determines the polularity of different Smartphones.");
}
}
communication.Poll:
package communication;
import java.util.Scanner;
import calculations.QuestionItem;
public class Poll {
private String questionTitle;
private QuestionItem[] questionItems;
private int count;
private Scanner in = new Scanner(System.in);
public Poll(String s,int arraySize){
questionTitle = s;
questionItems = new QuestionItem[arraySize]; //<--problem here?
}
public void addQuestionItem(String s){
if(count<questionItems.length){
questionItems[count++].setItemText(s); // <--exception here
}
}
public void askQuestions(String topic){
System.out.println(topic);
System.out.println(questionTitle);
for(int i=0; i<questionItems.length; i++){
System.out.println("- - - "+ questionItems[i].getItemText() +" - - -");
System.out.print("Your numerical answer: ");
questionItems[i].vote(in.nextInt());
}
}
void evaluation(){
//not ready :)
}
}
calculation.QuestionItem:
package calculation;
public class QuestionItem {
int count;
int overall;
String text;
public void vote (int pointValue){
overall += pointValue;
count++;
}
public double getDurchschnitt(){
return (double) overall/count;
}
public void setItemText(String s){
text = s;
}
public String getItemText(){
return text;
}
}
When you initialize an array of objects like this:
questionItems = new QuestionItem[arraySize];
All of the values are null by default.
In addQuestionItem, you try to call a method on an object in the array. However, that object starts off null, so this line of code doesn't work:
questionItems[count++].setItemText(s);
What you have to do is initialize the object before setting the text:
questionItems[count] = new QuestionItem();
questionItems[count].setItemText(s);
count++;
Alternatively, you can do what Constant suggested, and initialize all the objects when you initialize the array.
By the looks of it, you're making the array but it doesn't contain the objects yet. You probably want this in the constructor instead.
questionItems = new QuestionItem[arraySize];
for(int i = 0; i < questionItems.length; i++) {
questionItems[i] = new QuestionItem();
}
I am implementing an application using concurrent hash maps. It is required that one thread adds data into the CHM, while there is another thread that copies the values currently in the CHM and erases it using the clear() method. When I run it, after the clear() method is executed, the CHM always remains empty, though the other thread continues adding data to CHM.
Could someone tell me why it is so and help me find the solution.
This is the method that adds data to the CHM. This method is called from within a thread.
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
public static ConcurrentMap<String, String> updateJobList = new ConcurrentHashMap<String, String>(8, 0.9f, 6);
public void setUpdateQuery(String ticker, String query)
throws RemoteException {
dataBaseText = "streamming";
int n = 0;
try {
updateJobList.putIfAbsent(ticker, query);
}
catch(Exception e)
{e.printStackTrace();}
........................
}
Another thread calls the track_allocation method every minute.
public void track_allocation()
{
class Track_Thread implements Runnable {
String[] track;
Track_Thread(String[] s)
{
track = s;
}
public void run()
{
}
public void run(String[] s)
{
MonitoringForm.txtInforamtion.append(Thread.currentThread()+"has started runnning");
String query = "";
track = getMaxBenefit(track);
track = quickSort(track, 0, track.length-1);
for(int x=0;x<track.length;x++)
{
query = track[x].split(",")[0];
try
{
DatabaseConnection.insertQuery(query);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
joblist = updateJobList.values();
MonitoringForm.txtInforamtion.append("\nSize of the joblist is:"+joblist.size());
int n = joblist.size()/6;
String[][] jobs = new String[6][n+6];
MonitoringForm.txtInforamtion.append("number of threads:"+n);
int i = 0;
if(n>0)
{
MonitoringForm.txtInforamtion.append("\nSize of the joblist is:"+joblist.size());
synchronized(this)
{
updateJobList.clear();
}
Thread[] threads = new Thread[6];
Iterator it = joblist.iterator();
int k = 0;
for(int j=0;j<6; j++)
{
for(k = 0; k<n; k++)
{
jobs[j][k] = it.next().toString();
MonitoringForm.txtInforamtion.append("\n\ninserted into queue:\n"+jobs[j][k]+"\n");
}
if(it.hasNext() && j == 5)
{
while(it.hasNext())
{
jobs[j][++k] = it.next().toString();
}
}
threads[j] = new Thread(new Track_Thread(jobs[j]));
threads[j].start();
}
}
}
I can see a glaring mistake. This is the implementation of your Track_Thread classes run method.
public void run()
{
}
So, when you do this:
threads[j] = new Thread(new Track_Thread(jobs[j]));
threads[j].start();
..... the thread starts, and then immediately ends, having done absolutely nothing. Your run(String[]) method is never called!
In addition, your approach of iterating the map and then clearing it while other threads are simultaneously adding is likely to lead to entries occasionally being removed from the map without the iteration actually seeing them.
While I have your attention, you have a lot of style errors in your code:
The indentation is a mess.
You have named your class incorrectly: it is NOT a thread, and that identifier ignores the Java identifier rule.
Your use of white-space in statements is inconsistent.
These things make your code hard to read ... and to be frank, they put me off trying to really understand it.
I have a List<String> called lines and a huge (~3G) Set<String> called voc. I need to find all lines from lines that are in voc. Can I do this multithreaded way?
Currently I have this straightforward code:
for(String line: lines) {
if (voc.contains(line)) {
// Great!!
}
}
Is there a way to search for few lines at the same time? May be there are existing solutions?
PS: I am using javolution.util.FastMap, because it behaves better during filling up.
Here is a possible implementation. Please note that error/interruption handling has been omitted but this might give you a starting point. I included a main method so you could copy and paste this into your IDE for a quick demo.
Edit: Cleaned things up a bit to improve readability and List partitioning
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ParallelizeListSearch {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<String> searchList = new ArrayList<String>(7);
searchList.add("hello");
searchList.add("world");
searchList.add("java");
searchList.add("debian");
searchList.add("linux");
searchList.add("jsr-166");
searchList.add("stack");
Set<String> targetSet = new HashSet<String>(searchList);
Set<String> matchSet = findMatches(searchList, targetSet);
System.out.println("Found " + matchSet.size() + " matches");
for(String match : matchSet){
System.out.println("match: " + match);
}
}
public static Set<String> findMatches(List<String> searchList, Set<String> targetSet) throws InterruptedException, ExecutionException {
Set<String> locatedMatchSet = new HashSet<String>();
int threadCount = Runtime.getRuntime().availableProcessors();
List<List<String>> partitionList = getChunkList(searchList, threadCount);
if(partitionList.size() == 1){
//if we only have one "chunk" then don't bother with a thread-pool
locatedMatchSet = new ListSearcher(searchList, targetSet).call();
}else{
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
CompletionService<Set<String>> completionService = new ExecutorCompletionService<Set<String>>(executor);
for(List<String> chunkList : partitionList)
completionService.submit(new ListSearcher(chunkList, targetSet));
for(int x = 0; x < partitionList.size(); x++){
Set<String> threadMatchSet = completionService.take().get();
locatedMatchSet.addAll(threadMatchSet);
}
executor.shutdown();
}
return locatedMatchSet;
}
private static class ListSearcher implements Callable<Set<String>> {
private final List<String> searchList;
private final Set<String> targetSet;
private final Set<String> matchSet = new HashSet<String>();
public ListSearcher(List<String> searchList, Set<String> targetSet) {
this.searchList = searchList;
this.targetSet = targetSet;
}
#Override
public Set<String> call() {
for(String searchValue : searchList){
if(targetSet.contains(searchValue))
matchSet.add(searchValue);
}
return matchSet;
}
}
private static <T> List<List<T>> getChunkList(List<T> unpartitionedList, int splitCount) {
int totalProblemSize = unpartitionedList.size();
int chunkSize = (int) Math.ceil((double) totalProblemSize / splitCount);
List<List<T>> chunkList = new ArrayList<List<T>>(splitCount);
int offset = 0;
int limit = 0;
for(int x = 0; x < splitCount; x++){
limit = offset + chunkSize;
if(limit > totalProblemSize)
limit = totalProblemSize;
List<T> subList = unpartitionedList.subList(offset, limit);
chunkList.add(subList);
offset = limit;
}
return chunkList;
}
}
Simply splitting lines among different threads would (in Oracle JVM at least) spread the work into all CPUs if you are looking for this.
I like using CyclicBarrier, makes those threads controlled in an easier way.
http://javarevisited.blogspot.cz/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.html
It's absolutely possible to parallelize this using multiple threads. You could do the following:
Break up the list into a different "blocks," one per thread that will do the search.
Have each thread look over its block, checking whether each string is in the set, and if so adding the string to the resulting set.
For example, you might have the following thread routine:
public void scanAndAdd(List<String> allStrings, Set<String> toCheck,
ConcurrentSet<String> matches, int start, int end) {
for (int i = start; i < end; i++) {
if (toCheck.contains(allStrings.get(i))) {
matches.add(allStrings.get(i));
}
}
}
You could then spawn off as many threads as you needed to run the above method and wait for all of them to finish. The resulting matches would then be stored in matches.
For simplicity, I've had the output set be a ConcurrentSet, which automatically eliminates race conditions due to writes. Since you are only doing reads on the list of strings and set of strings to check for, no synchronization is required when reading from allStrings or performing lookups in toCheck.
Hope this helps!
Another option would be to use Akka, it does these kinds of things quite simply.
Actually, having done some search work with Akka, one of the things I can tell you about this too is that it supports two ways of parallelizing such things: through Composable Futures or Agents. For what you want, the Composable Futures would be completely sufficient. Then, Akka is actually not adding that much: Netty is providing the massively parallel io infrastructure, and Futures are part of the jdk, but Akka does make it super simple to put these two together and extend them when/if needed.
Update: It was a static buried deep in some code where it was used for just a couple of instructions. Thank you all for the suggestions.
We are not using one HashMap across threads (yes that is bad for many reasons). Each thread has its own HashMap.
We have a class that extends from Thread. In Thread.run() we create a HashMap, set a key/value pair in it, and pass that HashMap to a method. That method retrieves the value from the HashMap, inserts it into a string, and returns the string.
Sometimes the returned string has a different value (still in Thread.run()). This only occurs on hardware with 3+ physical cores. And it has only happened twice (before we added logging to help us find exactly what is going on of course).
Any idea why this would occur.
Update: here's the full code. The ProcessTxt is what pulls the value from the HashMap and puts it in the string.
import java.io.*;
import java.util.HashMap;
import junit.framework.TestCase;
import net.windward.datasource.dom4j.Dom4jDataSource;
import net.windward.xmlreport.ProcessReport;
import net.windward.xmlreport.ProcessTxt;
/**
* Test calling from multiple threads
*/
public class TestThreads extends TestCase {
private static String path = ".";
// JUnit stuff
public TestThreads(String name) {
super(name);
}
// Get logging going - called before any tests run
protected void setUp() throws Exception {
ProcessReport.init();
}
// this is not necessary - called after any tests are run
protected void tearDown() {
}
private static final int NUM_THREADS = 100;
private boolean hadWithVarError = false;
/**
* Test that each thread has unique variables.
*/
public void testRunReportsWithVariables() throws Exception {
// run 10 threads
ReportThreadWithVariables[] th = new ReportThreadWithVariables[NUM_THREADS];
for (int ind = 0; ind < NUM_THREADS; ind++) {
th[ind] = new ReportThreadWithVariables(this, ind);
th[ind].setName("Run " + ind);
}
for (int ind = 0; ind < NUM_THREADS; ind++)
th[ind].start();
boolean allDone = false;
while (!allDone) {
Thread.sleep(100);
allDone = true;
for (int ind = 0; ind < NUM_THREADS; ind++)
if (th[ind].isAlive())
allDone = false;
}
assertTrue(!hadWithVarError);
}
public static class ReportThreadWithVariables extends Thread {
private TestThreads obj;
private int num;
public ReportThreadWithVariables(TestThreads tt, int num) {
obj = tt;
this.num = num;
}
public void run() {
try{
System.out.println("starting " + num);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProcessTxt pt = new ProcessTxt(new FileInputStream(new File(path, "Thread_Test.docx")), out);
pt.processSetup();
// don't use order1.xml, but need a datasource.
Dom4jDataSource datasource = new Dom4jDataSource(new FileInputStream(new File(path, "order1.xml")));
HashMap map = new HashMap();
map.put("num", new Integer(num));
datasource.setMap(map);
pt.processData(datasource, "");
pt.processComplete();
String result = out.toString().trim();
System.out.println("complete " + num + ", result = " + result);
String expected = "Number: " + num;
if (!result.equals( expected ))
obj.hadWithVarError = true;
assertEquals(expected, result);
} catch (Throwable e) {
obj.hadWithVarError = true;
e.printStackTrace();
}
}
}
}
(edit to format code)
Given the lack of code and based solely on what has been written I am going to hypothesize that something is static. That is, somewhere along the line a static member is being stored to/written from.
num is not mutable and the other variables (string, map) are local so ReportThreadWithVariables looks thread safe. It seems to me that the problem is in the calls to external objects rather than what you posted.
Are the classes you use documented as Thread Safe?
For exampel, the javadoc of the processData method states that it should not be called multiple times for the same datasource which you seem to be doing (same file name).
ps: (not related) you could use a CountDownLatch instead of the while loop.
My code runs an algorithm on an array, and stores the results in an ArrayList. The problem is that I am not able to access the contents of the ArrayList for subsequent processing. Though my actual code is thousands of lines long, I have trapped the problem, and have re-created the problem in the short code segments below. You can take the three classes below and run them in your IDE without changes to reproduce the problem yourself. As you can see, it populates the ArrayList within makeArrayList.java, but yet the contents of the ArrayList are not subsequently visible in getArrayList.java.
Can anyone show me how to fix the code below so that the contents of the ArrayList become visible/usable in getArrayList.java and in myGUI.java?
Here is the code for the three classes:
The code for myGUI.java is:
package arrayListPractice;
import java.awt.Dimension;
import javax.swing.JFrame;
public class myGUI extends JFrame {
public myGUI() {
super("test GUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(300, 200));
getArrayList getArrList = new getArrayList();
getArrList.getPeaks();
this.pack();}
public static void main(String args[]) {
myGUI myFrame = new myGUI();
myFrame.setVisible(true);}}
The code for getArrayList.java is:
package arrayListPractice;
import java.util.*;
public class getArrayList {
public static ArrayList<Integer> PeakList;
int myLength = 3500;
double[] myArray=new double[myLength];
public ArrayList<Integer> getPeaks(){
for(int h=0;h<myLength;h++){myArray[h]=Math.sqrt((double)h);}
PeakList = new makeArrayList(myArray,myLength);
System.out.println("in getArrayList.getPeaks, PeakList.size() is: "+PeakList.size());
return PeakList;}}
The code for makeArrayList.java is:
package arrayListPractice;
import java.util.*;
public class makeArrayList extends ArrayList<Integer> {
ArrayList<Integer> myArrayList= new ArrayList<Integer>();
public makeArrayList(double[] myArray, int arrayLength) {
// NOTE: My actual code does many transformations to myArray. The resulting myArrayList
// contains only 1/1000 of the points in myArray. This code is just simplified for debugging.
for(int i=0;i<arrayLength;i++){myArrayList.add((int)Math.pow(myArray[i],2));}
System.out.println("in makeArrayList, PeakList.size() is: "+myArrayList.size());}}
You are confusing and combining inheritance and composition in the same class:
class makeArrayList extends ArrayList<Integer> {
ArrayList<Integer> myArrayList = new ArrayList<Integer>();
public makeArrayList(double[] myArray, int arrayLength) {
// NOTE: My actual code does many transformations to myArray. The
// resulting myArrayList
// contains only 1/1000 of the points in myArray. This code is just
// simplified for debugging.
for (int i = 0; i < arrayLength; i++) {
myArrayList.add((int) Math.pow(myArray[i], 2));
}
System.out.println("in makeArrayList, PeakList.size() is: "
+ myArrayList.size());
}
}
Note that this class both contains an ArrayList and extends ArrayList and you're trying to make both ArrayLists interchangeable , but they're not.
Some Suggestions:
There's no need for this class to extend ArrayList, so get rid of the extends and instead simplify and clarify tings by just using composition.
Avoid using static anything unless you have a very good reason for doing so. This is not part of your main problem, but is a problem with your sample code.
For the sake of others reading your code and either helping you or grading you, don't be afraid to use whitespace to make your code more readable. Page real estate is not that expensive. Also read up on and use Java naming conventions including capitalizing the first letter of class names. This will make it much easier for others (us!) to read and understand your code.
e.g.,
import java.util.ArrayList;
public class MyNonGUI2 {
public static void main(String args[]) {
GetArrayList2 getArrList = new GetArrayList2();
getArrList.getPeaks();
}
}
class GetArrayList2 {
public ArrayList<Integer> PeakList;
int myLength = 3500;
double[] myArray = new double[myLength];
public ArrayList<Integer> getPeaks() {
for (int h = 0; h < myLength; h++) {
myArray[h] = Math.sqrt((double) h);
}
PeakList = new MakeArrayList2(myArray, myLength).getArrayList();
System.out.println("in GetArrayList2.getPeaks, PeakList.size() is: "
+ PeakList.size());
return PeakList;
}
}
class MakeArrayList2 {
ArrayList<Integer> myArrayList = new ArrayList<Integer>();
public MakeArrayList2(double[] myArray, int arrayLength) {
for (int i = 0; i < arrayLength; i++) {
myArrayList.add((int) Math.pow(myArray[i], 2));
}
System.out.println("in MakeArrayList2, PeakList.size() is: "
+ myArrayList.size());
}
public int size() {
return myArrayList.size();
}
public ArrayList<Integer> getArrayList() {
return new ArrayList<Integer>(myArrayList);
}
}