vishal.avad
BAN USERNo. Other threads will continue to run without impact.
You can test it with following program. It does not matter if its an Checked - Unchecked Exception / Error
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadLocalExample {
static int i=0;
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
ThreadLocal<String> threadLocal = new ThreadLocal<String>();
ThreadLocal<Long> threadLocalInt = new ThreadLocal<Long>();
threadLocal.set(Thread.currentThread().getName());
threadLocalInt.set(Thread.currentThread().getId());
while(true){
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I am Thread "+Thread.currentThread().getName()+ " with variable "+threadLocal.get()+" Integer "+threadLocalInt.get());
}
}
};
Runnable runnableException = new Runnable() {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
throw new Error("I am getting killed "+Thread.currentThread().getName());
}
};
ExecutorService service = Executors.newFixedThreadPool(5);
service.execute(runnableException);
service.execute(runnable);
service.execute(runnable);
service.execute(runnable);
service.execute(runnable);
service.shutdown();
}
}
I prepared this one with the TDD
Test Class
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FibonTest {
@Test
public void return0WhenFibon0(){
assertEquals(0,Fibon.fibo(0));
}
@Test
public void return1WhenFibon1(){
assertEquals(1,Fibon.fibo(1));
}
@Test
public void return3WhenFibon2(){
assertEquals(3,Fibon.fibo(2));
}
@Test
public void return6WhenFibon3(){
assertEquals(6,Fibon.fibo(3));
}
@Test
public void return45WhenFibon9(){
assertEquals(45,Fibon.fibo(9));
}
}
Fibon
public class Fibon {
public static int fibo(int input) {
int result=0;
while(input>0){
result+=input--;
}
return result;
}
}
I found this one simpler
public class LargestString {
public static void main(String[] args) {
String[] arr={"careercu","career" ,"dewdeals" , "online","filewrit","filewrit"};
int maxLen=0;
for(String str:arr){
if(maxLen<str.length()){maxLen=str.length();}
}
for(String str:arr){
if(str.length()==maxLen){System.out.println(str);}
}
}
}
- vishal.avad May 22, 2014
Hi Nash, How did it go? What was the exercise about?
- vishal.avad June 09, 2014