importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;importjava.util.Random;importjava.util.concurrent.TimeUnit;publicclassTerminatingNormalNonBlockingThreadWithInterrupt{publicstaticvoidmain(Stringargc[])throwsInterruptedException{StringcurrentThreadName=Thread.currentThread().getName();System.out.println("["+currentThreadName+"]"+" thread starts here...");LoopTasktask1=newLoopTask();LoopTasktask2=newLoopTask();Threadthread1=newThread(task1,"Thread-1");Threadthread2=newThread(task2,"Thread-2");thread1.start();thread2.start();TimeUnit.MILLISECONDS.sleep(500);thread1.interrupt();thread2.interrupt();System.out.println("["+currentThreadName+"]"+" thread ends here...");}}classLoopTaskimplementsRunnable{privatestaticintcount=0;privateintid;privateStringtaskId;privatefinalintDATA_SIZE=100000;@Overridepublicvoidrun(){StringcurrentThreadName=Thread.currentThread().getName();System.out.println("#### <"+currentThreadName+","+taskId+"> starting...####");while(true){System.out.println("<"+currentThreadName+","+taskId+"> TICK TICK");doSomeWork();//Interrupt can not work with timer because the timer is a blocking functionif(Thread.interrupted()){//Thread.interrupted()break;}}System.out.println("#### <"+currentThreadName+","+taskId+"> done...####");}publicLoopTask(){this.id=++count;this.taskId="Task-"+id;}privatevoiddoSomeWork(){for(inti=0;i<2;i++){Collections.sort(generateDataSet());}}privateList<Integer>generateDataSet(){List<Integer>intList=newArrayList<>();RandomrandomGenerator=newRandom();for(inti=0;i<DATA_SIZE;i++){intList.add(randomGenerator.nextInt(DATA_SIZE));}returnintList;}}