Quizzes
Site Language: English
Українська
English
Русский
Programming Tests
Login
Sign Up
Programming Tests
Theory
Snippets
Papers
Landing
Android
Prices
FAQ
Cosmo Story
Terms and Conditions
Privacy Policy
Cookies Policy
Send Feedback
multithreading
:
Content language: English
Русский
Given the two examples below, what happens if they are compiled and executed? Example №1: public class Main { public static void main(String[] args) { new Thread(){ { this.setDaemon(true); } public void run() { while(true) { System.out.println("Thread is running!"); } } }.start(); } } Example №2: public class Main { public static void main(String[] args) { new Thread() { public void run() { while(true) { System.out.println("Thread is running!"); } } }.start(); } }
multithreading
What will be the result of the following code compilation and execution? public class Test { private static class Resource { public int value; }; private Resource resourceA = new Resource(); private Resource resourceB = new Resource(); public int read() { synchronized (resourceA) { synchronized (resourceB) { return resourceA.value + resourceB.value; } } } public void write(int a, int b) { synchronized (resourceB) { synchronized (resourceA) { resourceA.value = a; resourceB.value = b; } } } public static void main(String[] args) { final Test test = new Test(); Runnable targetA = new Runnable() { public void run() { while (true) System.out.println(test.read()); } }; Runnable targetB = new Runnable() { public void run() { while (true) test.write(1, 2); } }; new Thread(targetA).start(); new Thread(targetB).start(); } }
multithreading
What will be the result of compilation and execution of the following code? public class Test extends Thread { public static void delay(long t) { try { Thread.sleep(t); } catch (InterruptedException e) { System.out.print("Ex-A "); } } public void run() { delay(1000); halt(); } public void halt() { try { this.wait(); } catch (Exception e) { System.out.print("Ex-B "); } } public static void main(String args[]) throws Exception { Test test = new Test(); Thread t = new Thread(test); t.start(); delay(100); test.interrupt(); delay(2000); t.notifyAll(); } }
multithreading
← Prev
1
2
Next →
Sign Up Now
or
Subscribe for future quizzes