Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class ThreadStopTest {
- public static void main(String[] args) {
- Thread unstoppableThread = new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- final byte[] content = new byte[1000];
- for (int i = 0; i < 3000000; i++) {
- // do nothing -> just to pollute memory
- // and perdoem a decently expensive operation
- StringBuilder builder = new StringBuilder();
- int start = new Random().nextInt(content.length - 10);
- builder.append(new String(content).substring(start, start + 10));
- builder.toString();
- }
- System.out.println("and another iteration...");
- }
- }
- });
- unstoppableThread.start();
- unstoppableThread.interrupt();
- try {
- unstoppableThread.join();
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("FINISHED");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement