Java for Beginner
715 subscribers
657 photos
174 videos
12 files
1.03K links
Канал от новичков для новичков!
Изучайте Java вместе с нами!
Здесь мы обмениваемся опытом и постоянно изучаем что-то новое!

Наш YouTube канал - https://www.youtube.com/@Java_Beginner-Dev

Наш канал на RUTube - https://rutube.ru/channel/37896292/
Download Telegram
Что выведет код?

public class Task230725 {
private static volatile boolean flag = true;

public static void main(String[] args) {
new Thread(() -> {
while (flag) {
// empty loop
}
System.out.println("Thread stopped");
}).start();

try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}

flag = false;
System.out.println("Main stopped");
}
}


#Tasks
👍2
Что выведет код?

public class Task240725 {
private static synchronized void print(String msg) {
System.out.print(msg + " ");
}

public static void main(String[] args) {
Thread t1 = new Thread(() -> print("Hello"));
Thread t2 = new Thread(() -> print("World"));

t1.start();
t2.run();

try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


#Tasks
👍2
Что выведет код?

import java.util.concurrent.locks.ReentrantLock;

public class Task250725 {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
try {
lock.lock();
System.out.println("First lock");
lock.lock();
System.out.println("Second lock");
} finally {
lock.unlock();
}
}
}


#Tasks
👍1
Что выведет код?

import java.util.concurrent.*;

public class Task280725 {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> {
Thread.sleep(1000);
return "Done";
});

System.out.println(future.get(500, TimeUnit.MILLISECONDS));
executor.shutdown();
}
}


#Tasks
🔥3
Что выведет код?

public class Task290725 {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();

new Thread(() -> {
synchronized(lock1) {
synchronized(lock2) {
System.out.println("Thread 1");
}
}
}).start();

new Thread(() -> {
synchronized(lock1) {
synchronized(lock2) {
System.out.println("Thread 2");
}
}
}).start();
}
}


#Tasks
👍1
Что выведет код?

public class Task300725 {
public static void main(String[] args) {
Object lock = new Object();

synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

System.out.println("Done");
}
}


#Tasks
👍1
Что выведет код?

import java.util.concurrent.*;

public class Task310725 {
public static void main(String[] args) throws Exception {
Callable<String> task = () -> "Result";
FutureTask<String> future = new FutureTask<>(task);

new Thread(future).start();
System.out.println(future.get());
}
}


#Tasks
👍4
Что выведет код?

import java.util.concurrent.CountDownLatch;

public class Task010825 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);

new Thread(() -> {
latch.countDown();
latch.countDown();
}).start();

latch.await();
System.out.println("Completed");
}
}


#Tasks
👍1