Given the code below - how the readAllValuesFuture() method should be implemented?
Make a call and depending on the result of its Future:
either make the next call and pass the result of the previous one
or return an aggregated result of all previous calls.
package com.example.demo;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class TestFuture
{
#Test
public void testReadAllValues() throws Exception
{
counter = 0;
var list = readAllValues();
assert list.size() == 10;
assert counter == 11;
}
#Test
public void testReadAllValuesFuture() throws Exception
{
counter = 0;
var list = readAllValuesFuture().get();
assert list.size() == 10;
assert counter == 11;
}
// Synchronous logic - plain and simple
private List<Integer> readAllValues()
{
List<Integer> list = new ArrayList<>();
Integer res = makeFirstRequest();
while (res != null)
{
list.add(res);
res = makeNextRequest(res);
}
return list;
}
// Futures - how to implement it?
private CompletableFuture<List<Integer>> readAllValuesFuture()
{
return CompletableFuture.failedFuture(new UnsupportedOperationException());
}
private int counter = 0;
// Assuming this is external code which cannot be changed
private Integer makeFirstRequest()
{
counter++;
return 0;
}
// Assuming this is external code which cannot be changed
private Integer makeNextRequest(Integer prevValue)
{
counter++;
if (++prevValue < 10)
{
return prevValue;
}
return null;
}
// Assuming this is external code which cannot be changed
private CompletableFuture<Integer> makeFirstRequestFuture()
{
counter++;
return CompletableFuture.completedFuture(0);
}
// Assuming this is external code which cannot be changed
private CompletableFuture<Integer> makeNextRequestFuture(Integer prevValue)
{
counter++;
if (++prevValue < 10)
{
return CompletableFuture.completedFuture(prevValue);
}
return CompletableFuture.completedFuture(null);
}
}
Real-life use-case:
Implement listAllTables() from the example below using DynamoDbAsyncClient (DynamoDbAsyncClient.listTables() returns CompletableFuture<ListTablesResponse> instead of ListTablesResponse as DynamoDbClient.listTables() does):
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-dynamodb-tables.html
One way to achieve this is via Stream.iterate:
var values = Stream.iterate(
makeFirstRequestFuture().get(),
Objects::nonNull,
previous -> {
try {
return makeNextRequestFuture(previous).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
).toList();
Related
Receiving an exception "BufferOverflowException" while running sikuli script via threads. The same script works properly without thread object. I have no clue what causes the bufferoverflowexception. Please guide.
Sample Code:-
import org.sikuli.script.*; // Library import
public class Sikuli {
private Screen screen = new Screen();
private Pattern getPattern(String imagePath) {
return new Pattern(imagePath);
}
public void click(String imagePath) throws FindFailed, InterruptedException {
screen.click(getPattern(imagePath));
}
public void sendKeys(String value) {
screen.type(value);
}
import Sukuli // Custom sikuli class import
public class foo {
Sikuli sk = new Sikuli();
public void test() {
Thread t1 = new Thread(() -> {
sk.click(new Pattern("imagePath"));
sk.type("fooboo");
});
t1.start();
}
}
From Sikuli Library Methods which are being referred by my class methods:-
public int type(String text) {
try {
return this.keyin((Object)null, text, 0);
} catch (FindFailed var3) {
return 0;
}
}
private <PFRML> int keyin(PFRML target, String text, int modifiers) throws FindFailed {
if (target != null && 0 == this.click(target, 0)) {
return 0;
} else {
Debug profiler = Debug.startTimer("Region.type", new Object[0]);
if (text != null && !"".equals(text)) {
String showText = "";
for(int i = 0; i < text.length(); ++i) {
showText = showText + Key.toJavaKeyCodeText(text.charAt(i));
}
String modText = "";
String modWindows = null;
if ((modifiers & 4) != 0) {
modifiers -= 4;
modifiers |= 4;
log(3, "Key.WIN as modifier", new Object[0]);
modWindows = "Windows";
}
if (modifiers != 0) {
modText = String.format("( %s ) ", KeyEvent.getKeyModifiersText(modifiers));
if (modWindows != null) {
modText = modText.replace("Meta", modWindows);
}
}
Debug.action("%s TYPE \"%s\"", new Object[]{modText, showText});
log(3, "%s TYPE \"%s\"", new Object[]{modText, showText});
profiler.lap("before getting Robot");
IRobot r = this.getRobotForRegion();
int pause = 20 + (Settings.TypeDelay > 1.0 ? 1000 : (int)(Settings.TypeDelay * 1000.0));
Settings.TypeDelay = 0.0;
profiler.lap("before typing");
r.typeStarts();
for(int i = 0; i < text.length(); ++i) {
r.pressModifiers(modifiers);
r.typeChar(text.charAt(i), KeyMode.PRESS_RELEASE);
r.releaseModifiers(modifiers);
r.delay(pause);
}
r.typeEnds();
profiler.lap("after typing, before waitForIdle");
r.waitForIdle();
profiler.end();
return 1;
} else {
return 0;
}
}
}
Trace logs:-
[log] CLICK on L[1517,53]#S(0) (555 msec)[log] TYPE "fooboo"Exception in thread "Thread-4" java.nio.BufferOverflowException
at java.nio.Buffer.nextPutIndex(Buffer.java:521)
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:169)
at org.apache.maven.surefire.api.stream.AbstractStreamEncoder.encodeString(AbstractStreamEncoder.java:127)
at org.apache.maven.surefire.api.stream.AbstractStreamEncoder.encodeStringData(AbstractStreamEncoder.java:171)
at org.apache.maven.surefire.api.stream.AbstractStreamEncoder.encode(AbstractStreamEncoder.java:157)
at org.apache.maven.surefire.booter.spi.EventChannelEncoder.encodeMessage(EventChannelEncoder.java:398)
at org.apache.maven.surefire.booter.spi.EventChannelEncoder.setOutErr(EventChannelEncoder.java:188)
at org.apache.maven.surefire.booter.spi.EventChannelEncoder.testOutput(EventChannelEncoder.java:183)
at org.apache.maven.surefire.api.booter.ForkingRunListener.writeTestOutput(ForkingRunListener.java:113)
at org.apache.maven.surefire.api.booter.ForkingRunListener.writeTestOutput(ForkingRunListener.java:44)
at org.apache.maven.surefire.common.junit4.JUnit4RunListener.writeTestOutput(JUnit4RunListener.java:235)
at org.apache.maven.surefire.api.report.ConsoleOutputCapture$ForwardingPrintStream.println(ConsoleOutputCapture.java:144)
at org.sikuli.basics.Debug.log(Debug.java:881)
at org.sikuli.basics.Debug.action(Debug.java:645)
at org.sikuli.script.Region.keyin(Region.java:4613)
at org.sikuli.script.Region.type(Region.java:4493)
at Sikuli.sendKeys(Sikuli.java:87)
I was trying to make a method that returns true if given "Strings" are anagrams. unfortunately i cant even test it and i don know what is wrong. The markers at left says:
Multiple markers at this line
- Breakpoint:Test
- Duplicate modifier for the
type Test
Here is the source code:
package zajecia19;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
public
public class Test {
public static boolean Anagraamy(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s1.length(); i++) {
if (map.containsKey(s1.charAt(i))) {
map.put(s1.charAt(i), map.get(s1.charAt(i)) + 1);
} else {
map.put(s1.charAt(i), 1);
}
if (map.containsKey(s2.charAt(i))) {
map.put(s2.charAt(i), map.get(s2.charAt(i)) - 1);
} else {
map.put(s2.charAt(i), -1);
}
}
for( Integer value: map.values()){
if(value != 0 ){
return false;
}
}
return true;
}
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("slowa2"))) {
System.out.println( Anagraamy("abba", "babb"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Because you have
public
public
there.
The obvious way to fix that: remove the first one. And next time: pay attention to what the compiler is trying to tell you.
I'm doing a snake game, and I want after the game is over, the score is saved in a file
I did this, but there's error in the two lines that sends the value to the Testing Class
private boolean isGameOver(int headLocX, int headLocY) {
for (int i = SNAKE_LENGTH_DEFAULT; i < totalBodyPart - 2; i++) {
Point partLoc = snakeBodyPart[i].getLocation();
if (partLoc.equals(new Point(headLocX, headLocY))) {
Testing test = new Testing(score);
return true;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Testing
{
public Testing(int score)
{
File file = new File("C:\\Users\\Eng. Mohammed\\Desktop\\Snake 1.0.1\\Snake\\Score.Txt");
PrintWriter printWriter = null;
try
{
printWriter = new PrintWriter(file);
printWriter.println(score);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
if ( printWriter != null )
{
printWriter.close();
}
}
}
}
Change this method
private boolean isGameOver(int headLocX, int headLocY) {
for (int i = SNAKE_LENGTH_DEFAULT; i < totalBodyPart - 2; i++) {
Point partLoc = snakeBodyPart[i].getLocation();
if (partLoc.equals(new Point(headLocX, headLocY))) {
Testing test = new Testing();
Testing(score);//It is an invalid call no method is defined like this or a class can't be initialized like this.
return true;
}
}
TO
private boolean isGameOver(int headLocX, int headLocY) {
for (int i = SNAKE_LENGTH_DEFAULT; i < totalBodyPart - 2; i++) {
Point partLoc = snakeBodyPart[i].getLocation();
if (partLoc.equals(new Point(headLocX, headLocY))) {
Testing test = new Testing(score);//This is the correct way to initialize the Testing class.
return true;
}
}
I m trying to run a pig script which is calling a User Defined Function written in java.I m trying to test this script with a very small file of 264Bytes. I end up getting java heap space errors and the job fails. I have tried running the job with the -Xms1024M option, it runs for the smaller files but fails with a larger file.
And even then my cluster is powerful enough to not trip over such small files, I wonder how i can fix this memory leak.
Can someone pls help,
import java.util.HashMap;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.text.*;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.*;
import com.tictactec.ta.lib.CoreAnnotated;
import com.tictactec.ta.lib.MAType;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import com.tictactec.ta.lib.meta.annotation.InputParameterInfo;
import com.tictactec.ta.lib.meta.annotation.InputParameterType;
import com.tictactec.ta.lib.meta.annotation.OptInputParameterInfo;
import com.tictactec.ta.lib.meta.annotation.OptInputParameterType;
import com.tictactec.ta.lib.meta.annotation.OutputParameterInfo;
import com.tictactec.ta.lib.meta.annotation.OutputParameterType;
public class taLib extends EvalFunc<DataBag>
{
private static final int MIN_ARGS = 3;
public static CoreAnnotated core = new CoreAnnotated();
private static Method func_ref = null;
public DecimalFormat df = new DecimalFormat("#.###");
public DataBag exec(Tuple args) throws IOException
{
DataBag input=null;
MInteger outStart = new MInteger();
MInteger outLen = new MInteger();
Map<String,Object>outputParams=new HashMap<String, Object>();
String func_name;
List<Integer> ip_colmns= new ArrayList<Integer>();
List<double[]>ip_list=new ArrayList<double[]>();
List<String>opt_type=new ArrayList<String>();
List<Object>opt_params=new ArrayList<Object>();
//////
long m1=Runtime.getRuntime().freeMemory();
System.out.println(m1);
long m2=Runtime.getRuntime().totalMemory();
System.out.println(m2);
//////
int ip_noofparams=0;
int op_noofparams=0;
int opt_noofparams=0;
if (args == null || args.size() < MIN_ARGS)
throw new IllegalArgumentException("talib: must have at least " +
MIN_ARGS + " args");
if(args.get(0) instanceof DataBag)
{input = (DataBag)args.get(0);}
else{throw new IllegalArgumentException("Only a valid bag name can be
passed");}
// get no of fields in bag
Tuple t0=input.iterator().next();
int fields_in_bag=t0.getAll().size();
if(args.get(1) instanceof String)
{func_name = (String)args.get(1);}
else{throw new IllegalArgumentException("Only valid function name can be
passed at arg 1");}
func_ref=methodChk(func_name);
if (func_ref == null) {
throw new IllegalArgumentException("talib: function "
+ func_name + " was not found");
}
for (Annotation[] annotations : func_ref.getParameterAnnotations())
{
for (Annotation annotation : annotations)
{
if(annotation instanceof InputParameterInfo)
{
InputParameterInfo inputParameterInfo =
(InputParameterInfo)annotation;
if(inputParameterInfo.type().equals(InputParameterType.TA_Input_Price))
{
ip_noofparams=numberOfSetBits(inputParameterInfo.flags());
}
else
{
ip_noofparams++;
}
}
if(annotation instanceof OptInputParameterInfo)
{
OptInputParameterInfo optinputParameterInfo=
(OptInputParameterInfo)annotation;
opt_noofparams++;
if
(optinputParameterInfo.type().equals(OptInputParameterType.TA_OptInput_IntegerRange))
{
opt_type.add("Integer");
}
else
if(optinputParameterInfo.type().equals(OptInputParameterType.TA_OptInput_RealRange))
{
opt_type.add("Double");
}
else
if(optinputParameterInfo.type().equals(OptInputParameterType.TA_OptInput_IntegerList))
{
opt_type.add("String");
}
else{throw new IllegalArgumentException("whoopsie ...serious
mess in opt_annotations");}
}
if (annotation instanceof OutputParameterInfo)
{
OutputParameterInfo outputParameterInfo =
(OutputParameterInfo) annotation;
op_noofparams++;
if
(outputParameterInfo.type().equals(OutputParameterType.TA_Output_Real))
{
outputParams.put(outputParameterInfo.paramName(), new
double[(int) input.size()]);
}
else if
(outputParameterInfo.type().equals(OutputParameterType.TA_Output_Integer))
{
outputParams.put(outputParameterInfo.paramName(), new
int[(int)input.size()]);
}
}
}
}
int total_params =ip_noofparams+opt_noofparams;
if((args.size()-2)!=total_params){throw new IllegalArgumentException("Wrong
no of argumets passed to UDF");}
// get the ip colmns no's
for(int i=2;i<(2+ip_noofparams);i++)
{
if(args.get(i) instanceof Integer )
{
if((Integer)args.get(i)>=0 && (Integer)args.get(i)<fields_in_bag)
{
ip_colmns.add((Integer) args.get(i));
}
else{throw new IllegalArgumentException("The input colmn specified
is invalid..please enter a valid colmn no:0-"+(fields_in_bag-1));}
}
else{throw new IllegalArgumentException("Wrong arguments entered:
Only"+ip_noofparams+"field no's of type(integer) allowed for fn"+func_name ); }
}
// create a list of ip arrays
for(int i=0;i<ip_colmns.size();i++)
{
ip_list.add((double[]) Array.newInstance(double.class, (int)input.size()));
}
int z=0;
int x=0;
// fill up the arrays
for(Tuple t1: input)
{
Iterator<double[]> itr=ip_list.iterator();
z=0;
while(itr.hasNext())
{
if((Double)t1.get(ip_colmns.get(z)) instanceof Double)
{
((double[])itr.next())[x]=(Double) t1.get(ip_colmns.get(z++));
}
else{throw new IllegalArgumentException("Illegal argument while
filling up array...only double typr allowed");}
}
x++;
}
//deal with opt params
int s=0;
for(int i=(2+ip_noofparams);i<(2+ip_noofparams+opt_noofparams);i++)
{
if(opt_type.get(s).equalsIgnoreCase(args.get(i).getClass().getSimpleName().toString()))
{
if(opt_type.get(s).equalsIgnoreCase("String"))
{
String m=args.get(i).toString().toLowerCase();
String ma=m.substring(0, 1).toUpperCase();
String mac=m.substring(1);
String macd=ma+mac;
MAType type =MAType.valueOf(macd);
opt_params.add(type);
s++;
}
else{
opt_params.add(args.get(i));
s++;
}
}
else if(opt_type.get(s).equalsIgnoreCase("Double"))
{
if(args.get(i).getClass().getSimpleName().toString().equalsIgnoreCase("Integer"))
{
opt_params.add((Double)((Integer)args.get(i)+0.0));
s++;
}
else{throw new IllegalArgumentException("Opt arguments do
not match for fn:"+func_name+", pls enter opt arguments in right order"); }
}
else{throw new IllegalArgumentException("Opt arguments do not match
for fn:"+func_name+", pls enter opt arguments in right order");}
}
List<Object> ta_argl = new ArrayList<Object>();
ta_argl.add(new Integer(0));
ta_argl.add(new Integer((int)input.size() - 1));
for(double[]in: ip_list)
{
ta_argl.add(in);
}
if(opt_noofparams!=0)
{ta_argl.addAll(opt_params);}
ta_argl.add(outStart);
ta_argl.add(outLen);
for (Map.Entry<String, Object> entry : outputParams.entrySet())
{
ta_argl.add(entry.getValue());
}
RetCode rc = RetCode.Success;
try {
rc = (RetCode)func_ref.invoke(core, ta_argl.toArray());
} catch (Exception e)
{
assert false : "I died in ta-lib, but Java made me a zombie...";
}
assert rc == RetCode.Success : "ret code from " + func_name;
if (outLen.value == 0) return null;
//////
DataBag ret=null;
ret =outTA(input,outputParams,outStart);
outputParams.clear();
ip_list.clear();
opt_params.clear();
opt_type.clear();
ip_colmns.clear();
Runtime.getRuntime().gc();
return ret;
}
public DataBag outTA(DataBag bag,Map<String, Object> outputParams,MInteger outStart)
{
DataBag nbag=null;
TupleFactory mTupleFactory=TupleFactory.getInstance();
BagFactory mBagFactory=BagFactory.getInstance();
nbag=mBagFactory.newDefaultBag();
Tuple tw=bag.iterator().next();
int fieldsintup=tw.getAll().size();
for(Tuple t0: bag)
{
Tuple t1=mTupleFactory.newTuple();
for(int z=0;z<fieldsintup;z++)
{
try {
t1.append(t0.get(z));
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Ouch");
}
}
nbag.add(t1);
}
int i = 0;
int j=0;
for (Tuple t2: nbag)
{
if(i>=outStart.value)
{
for(Map.Entry<String,Object>entry: outputParams.entrySet())
{
t2.append(entry.getKey().substring(3).toString());
if(entry.getValue() instanceof double[])
{
t2.append( new Double
(df.format(((double[])entry.getValue())[j])));
}
else if(entry.getValue() instanceof int[])
{
t2.append( ((int[])entry.getValue())[j]);
}
else{throw new
IllegalArgumentException(entry.getValue().getClass()+"not supported");}
}
i++;j++;
}
else
{t2.append(0.0);
i++;
}
}
return nbag;
}
public Method methodChk(String fn)
{
String fn_name=fn;
Method tmp_fn=null;
for (Method meth: core.getClass().getDeclaredMethods())
{
if (meth.getName().equalsIgnoreCase(fn_name))
{
tmp_fn = meth;
break;
}
}
return tmp_fn;
}
public int numberOfSetBits(int i) {
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return ((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
}
Probably a problem with the BZip codec - the API does note that it's rather memory hungry:
http://hadoop.apache.org/common/docs/r0.20.0/api/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.html
The compression requires large amounts of memory
When you increased the memory with -Xms2048m did you set the options for the pig grunt shell, or for the map/reduce jobs?
set mapred.child.java.opts=-Xmx2048m
You can check by looking in the JobTracker, find the job that failed, open the job.xml and locate the value of mapred.child.java.opts
I have a program that creates a queue, enqueues objects to the queue and then dequeues them one by one if the queue is not empty. The problem I am having is that the queue comes up empty each time it is checked. Print is called on the queue after enqueuing each object and it prints the queue's contents just fine.
import java.util.*;
import java.io.*;
public class Processor2
{
private LinkedQueue queue = new LinkedQueue();
private int time = 0;
private int count = 100;
private int amount = 0;
private PrintWriter out;
private Person temp;
private boolean var;
private Random randomNum = new Random();;
private String turn;
private int popCount=0;
private int loopCount =0;
public void start()
{
amount = randomNum.nextInt(5);
amount += 5;
pop(amount, time);
sim();
}
public void pop(int num, int time)
{
for(int i=1; i<=num; i++)
{
Person pe = new Person(i, time, 0);
queue.enqueue(pe);
System.out.println(queue);
}
popCount += num;
}
public void sim()
{
try
{
out = new PrintWriter(new FileWriter("output.txt"));
while(loopCount<=100)
{
var = queue.isEmpty();
if(var=true)
{
System.out.println("queue is empty");
}
if(var=false)
{
Object temp = queue.dequeue();
double rand = Math.random();
if(rand < 0.5)
{
System.out.println("inside if else statement");
// does stuff with object //
loopCount++;
}
else
{
System.out.println("inside if else statement");
// does stuff with object //
loopCount++;
}
}
}
out.close();
}
catch (IOException ioe)
{
System.out.println("Error Writing to File: " + ioe);
}
}
}
there doesn't seem to be anything wrong with the queue's isEmpty() method, but here it is:
public boolean isEmpty()
{
if(count == 0)
{
return true;
}
else
{
return false;
}
}
var = queue.isEmpty();
if(var=true) // this line *sets* var to true
{
System.out.println("queue is empty");
}
If you change if (var=true) to if(var==true) or just if(var) you should see a different result