List of usage examples for java.util.concurrent FutureTask get
public V get() throws InterruptedException, ExecutionException
From source file:ru.valle.btc.MainActivityTest.java
private String getText(final Activity activity, final int id) { FutureTask<String> task = new FutureTask<>(new Callable<String>() { @Override// ww w . j a v a2 s. co m public String call() throws Exception { TextView textView = ((TextView) activity.findViewById(id)); return textView.getVisibility() == View.VISIBLE ? getString(textView) : null; } }); activity.runOnUiThread(task); try { return task.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return null; }
From source file:com.test.test.MultipleRequestsForServerTest.java
@Test public void testMultiClientsForServer() throws Exception { // Number of threads final int size = 30; LOG.debug("clientSimple1:" + clientSimple); List<IServiceSimple> serviceSimpleList = new ArrayList<IServiceSimple>(); for (int i = 0; i < size; i++) { IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); serviceSimpleList.add(proxyService); }// ww w. j a va 2 s . c om List<ClientCallableForServer> clientCallableList = new ArrayList<ClientCallableForServer>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallableForServer(serviceSimpleList.get(i), i)); } List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallableForServer clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); }
From source file:com.test.test.MultipleRequestsForServerTest.java
@Test public void testSingleClientsForServer() throws Exception { // Number of threads final int size = 20; LOG.debug("clientSimple1:" + clientSimple); IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); List<ClientCallableForServer> clientCallableList = new ArrayList<ClientCallableForServer>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallableForServer(proxyService, i)); }/*from w w w . j a v a 2 s .co m*/ List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallableForServer clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); LOG.debug("\n\n\n\n==========================="); LOG.debug("And just sleep for empty pool"); Thread.sleep(40000); IServiceSimple proxyService3 = clientSimple.getProxy(IServiceSimple.class); proxyService3.functionNumber1("1", "1"); }
From source file:com.luke.lukef.lukeapp.tools.LukeNetUtils.java
/** * Creates a Bitmap from the given URL/* w w w. j a v a2 s .c om*/ * * @param imageUrl The URL of the image * @return The image as an Bitmap object, otherwise <b>null</b> * @throws ExecutionException * @throws InterruptedException */ public Bitmap getBitmapFromURL(final String imageUrl) throws ExecutionException, InterruptedException { Callable<Bitmap> bitmapCallable = new Callable<Bitmap>() { @Override public Bitmap call() throws Exception { try { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); return BitmapFactory.decodeStream(input); } catch (IOException e) { Log.e(TAG, "call: ", e); return null; } } }; FutureTask<Bitmap> bitmapFutureTask = new FutureTask<>(bitmapCallable); Thread t = new Thread(bitmapFutureTask); t.start(); return bitmapFutureTask.get(); }
From source file:com.luke.lukef.lukeapp.tools.LukeNetUtils.java
/** * Sets a username for a user in the server * * @param username The username to be set * @return <b>true</b> if the request passes, <b>false</b> if it doesn't * @throws IOException/* w ww . j ava2 s . c o m*/ * @throws ExecutionException * @throws InterruptedException */ public boolean setUsername(final String username) throws IOException, ExecutionException, InterruptedException { Callable<Boolean> booleanCallable = new Callable<Boolean>() { @Override public Boolean call() throws Exception { HttpURLConnection httpURLConnection; URL setUsernameUrl = new URL( "http://www.balticapp.fi/lukeA/user/set-username?username=" + username); httpURLConnection = (HttpURLConnection) setUsernameUrl.openConnection(); httpURLConnection.setRequestProperty(context.getString(R.string.authorization), context.getString(R.string.bearer) + SessionSingleton.getInstance().getIdToken()); httpURLConnection.setRequestProperty(context.getString(R.string.acstoken), SessionSingleton.getInstance().getAccessToken()); return httpURLConnection.getResponseCode() == 200; } }; FutureTask<Boolean> booleanFutureTask = new FutureTask<>(booleanCallable); Thread t = new Thread(booleanFutureTask); t.start(); return booleanFutureTask.get(); }
From source file:com.test.test.ClientServerTest.java
@Test public void testMutliThreaded() throws Exception { // Number of threads final int size = 10; LOG.debug("clientSimple1:" + clientSimple); IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); List<ClientCallable> clientCallableList = new ArrayList<ClientCallable>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallable(proxyService, i)); }/* w w w.j a va 2s . c o m*/ List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallable clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); }
From source file:com.luke.lukef.lukeapp.tools.LukeNetUtils.java
/** * Fetches the newest link from the backend. This link will be shown at the start of the app. * Only the newest link will be shown/*from w w w. j a va 2 s .c om*/ * @return Link object of the newest link on the backend */ public Link getNewestLink() { Callable<Link> linkCallable = new Callable<Link>() { @Override public Link call() throws Exception { String jsonString = getMethod("http://www.balticapp.fi/lukeA/link"); JSONArray jsonArray = new JSONArray(jsonString); Log.e(TAG, "getlinks call: jsonArray" + jsonArray.toString()); List<Link> links = LukeUtils.parseLinksFromJsonArray(jsonArray); return links.get(links.size() - 1); } }; FutureTask<Link> linkFutureTask = new FutureTask<>(linkCallable); Thread t = new Thread(linkFutureTask); t.start(); try { return linkFutureTask.get(); } catch (InterruptedException | ExecutionException e) { Log.e(TAG, "reportSubmission: ", e); return null; } }
From source file:com.luke.lukef.lukeapp.tools.LukeNetUtils.java
/** * Sets a profile image for a user in the server * * @param bitmap The bitmap that should be set * @return <b>true</b> if the update passes, <b>false</b> if not *//*from ww w.ja v a 2 s. c o m*/ public boolean updateUserImage(final Bitmap bitmap) { Callable<Boolean> booleanCallable = new Callable<Boolean>() { @Override public Boolean call() throws Exception { HttpURLConnection conn; //create a json object from this submission to be sent to the server and convert it to string JSONObject jsonObject = new JSONObject(); jsonObject.put("image", LukeUtils.bitmapToBase64String(bitmap)); String urlParameters = jsonObject.toString(); return postMethod("http://www.balticapp.fi/lukeA/user/update", urlParameters); } }; FutureTask<Boolean> futureTask = new FutureTask<>(booleanCallable); Thread t = new Thread(futureTask); t.start(); try { return futureTask.get(); } catch (InterruptedException | ExecutionException e) { Log.e(TAG, "submitToServer: ", e); return false; } }
From source file:com.test.test.ClientServerTest.java
@Test public void testMutliThreadProxyClient() throws Exception { // Number of threads final int size = 20; LOG.debug("clientSimple1:" + clientSimple); List<IServiceSimple> serviceSimpleList = new ArrayList<IServiceSimple>(); for (int i = 0; i < size; i++) { IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); serviceSimpleList.add(proxyService); }//from ww w. j ava 2s .co m List<ClientCallable> clientCallableList = new ArrayList<ClientCallable>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallable(serviceSimpleList.get(i), i)); } List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallable clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); }
From source file:com.luke.lukef.lukeapp.tools.LukeNetUtils.java
/** * Report an inappropriate submissions by id * * @param submissionId The ID of the given submission. * @return <b>true</b> if the request passes, <b>false</b> if it doesn't. *//*from ww w. j a va 2s.c om*/ public String reportSubmission(final String submissionId) { Callable<String> booleanCallable = new Callable<String>() { @Override public String call() throws Exception { String jsonString = getMethod("http://www.balticapp.fi/lukeA/report/flag?id=" + submissionId); Log.e(TAG, "updateUserImage run: Result : " + jsonString); return "Error reporting"; } }; FutureTask<String> booleanFutureTask = new FutureTask<>(booleanCallable); Thread t = new Thread(booleanFutureTask); t.start(); try { return booleanFutureTask.get(); } catch (InterruptedException | ExecutionException e) { Log.e(TAG, "reportSubmission: ", e); return null; } }