Created
April 6, 2017 04:07
-
-
Save phg1024/99bb50a004e0d4a10397cc1f853cab9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.concurrent.*; | |
import java.util.Vector; | |
public class Main { | |
static class TimeConsumingTask implements Runnable { | |
public Vector<Integer> results = new Vector<>(); | |
@Override | |
public void run() { | |
System.out.println("Task thread: " + Thread.currentThread().getName()); | |
System.out.println("Running time consuming task: " + System.currentTimeMillis()); | |
try { | |
doSomething(0); | |
} catch(Exception e) { | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Exception caught. Printing stack trace ..."); | |
e.printStackTrace(); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Exit."); | |
return; | |
} | |
} | |
private int doSomething(int depth) throws Exception { | |
final long total_ints = 1<<20; | |
if(depth == 5) { | |
long numbers_to_gen = total_ints / (1<<depth); | |
int sum = 0; | |
for(int j=0;j<numbers_to_gen;++j) { | |
// Check if the thread is interrupted frequently | |
if(Thread.currentThread().isInterrupted()) throw new Exception("Compute task interrupted."); | |
sum += (new Random()).nextInt(2); | |
} | |
return sum; | |
} else if(depth == 0) { | |
for(int i=0;i<100;++i) { | |
int sum = doSomething(depth+1) + doSomething(depth+1); | |
results.add(sum); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Finished " + String.valueOf(i) + " / 100 @ " + System.currentTimeMillis()); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Current sum = " + sum); | |
} | |
return 0; | |
} else { | |
return doSomething(depth+1) + doSomething(depth+1); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("Main thread: " + Thread.currentThread().getName()); | |
long time_limit = (new Random()).nextInt(1000) + 500; | |
System.out.println("Timer limit: " + time_limit); | |
TimeConsumingTask compute_task = new TimeConsumingTask(); | |
Thread compute_thread = new Thread(compute_task); | |
System.out.println("Begin: " + System.currentTimeMillis()); | |
long start_time = System.currentTimeMillis(); | |
compute_thread.start(); | |
try { | |
// Don't over sleep | |
while((System.currentTimeMillis() - start_time) < time_limit * 0.98) { | |
// Sleep for some time | |
long elapsed_time = System.currentTimeMillis() - start_time; | |
long remaining_time = time_limit - elapsed_time; | |
long sleep_time = (long)Math.min(time_limit * 0.05, remaining_time * 0.9); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Elapsed time: " + elapsed_time + ", Sleep for " + sleep_time + " ms"); | |
Thread.currentThread().sleep(sleep_time); | |
} | |
compute_thread.interrupt(); | |
compute_thread.join(); | |
} catch(InterruptedException e){ | |
e.printStackTrace(); | |
} finally { | |
} | |
long end_time = System.currentTimeMillis(); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Compute task ended: " + end_time); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Total time used: " + (end_time - start_time)); | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Finding the maximum sum ..."); | |
int maxval = 0; | |
for(int i=0;i<compute_task.results.size();++i) { | |
maxval = Math.max(maxval, compute_task.results.get(i)); | |
} | |
System.out.println("[Thread " + Thread.currentThread().getName() + "] Max sum = " + maxval); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment