У меня есть следующий Java-код. Здесь я пытаюсь контролировать количество виртуальных потоков, изученных и вдохновленных этим потоком SO. Но все же я вижу отдельное и неконтролируемое количество идентификаторов потоков для каждой файловой операции. Чего мне здесь не хватает?
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class VirtualThreadDirectoryCopy4 {
public static void main(String[] args) throws Exception {
Path source = Path.of("C:\\source");
Path target = Path.of("C:\\source2");
int maxVirtualThreads = 2; // set your VT limit here
copyDirectory(source, target, maxVirtualThreads);
}
public static void copyDirectory(Path source, Path target, int maxThreads) throws Exception {
// Create a fixed-size virtual-thread executor
ExecutorService executor = Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().factory()
);
// Use a semaphore to limit concurrency
final Semaphore semaphore = new Semaphore(maxThreads);
try (executor) {
Files.walk(source).forEach(path -> {
Path relative = source.relativize(path);
Path dest = target.resolve(relative);
if (Files.isDirectory(path)) {
try {
Files.createDirectories(dest);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
executor.submit(() -> {
try {
semaphore.acquire();
copyFileWithThreadInfo(path, dest);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
semaphore.release();
}
});
}
});
}
}
private static void copyFileWithThreadInfo(Path src, Path dest) {
long start = System.nanoTime();
Thread t = Thread.currentThread();
String threadName = t.getName();
long threadId = t.threadId();
try {
Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
long end = System.nanoTime();
long millis = (end - start) / 1_000_000;
System.out.printf(
"Thread %s (ID %d) copied %s → %s in %d ms%n",
threadName, threadId, src, dest, millis
);
} catch (IOException e) {
System.err.printf(
"Thread %s (ID %d) failed to copy %s: %s%n",
threadName, threadId, src, e.getMessage()
);
}
}
}
Обновление:
На основе предложений и комментариев DuncG я придумал следующее:
Semaphore semaphore = new Semaphore(maxThreads);
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (Path file : files) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
executor.submit(() -> {
try {
copyFileTask(file, source, target);
} finally {
semaphore.release();
}
});
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... al-threads
Невозможно контролировать количество виртуальных потоков ⇐ JAVA
Программисты JAVA общаются здесь
-
Anonymous
1768424124
Anonymous
У меня есть следующий Java-код. Здесь я пытаюсь контролировать количество виртуальных потоков, изученных и вдохновленных этим потоком SO. Но все же я вижу отдельное и неконтролируемое количество идентификаторов потоков для каждой файловой операции. Чего мне здесь не хватает?
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class VirtualThreadDirectoryCopy4 {
public static void main(String[] args) throws Exception {
Path source = Path.of("C:\\source");
Path target = Path.of("C:\\source2");
int maxVirtualThreads = 2; // set your VT limit here
copyDirectory(source, target, maxVirtualThreads);
}
public static void copyDirectory(Path source, Path target, int maxThreads) throws Exception {
// Create a fixed-size virtual-thread executor
ExecutorService executor = Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().factory()
);
// Use a semaphore to limit concurrency
final Semaphore semaphore = new Semaphore(maxThreads);
try (executor) {
Files.walk(source).forEach(path -> {
Path relative = source.relativize(path);
Path dest = target.resolve(relative);
if (Files.isDirectory(path)) {
try {
Files.createDirectories(dest);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
executor.submit(() -> {
try {
semaphore.acquire();
copyFileWithThreadInfo(path, dest);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
semaphore.release();
}
});
}
});
}
}
private static void copyFileWithThreadInfo(Path src, Path dest) {
long start = System.nanoTime();
Thread t = Thread.currentThread();
String threadName = t.getName();
long threadId = t.threadId();
try {
Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
long end = System.nanoTime();
long millis = (end - start) / 1_000_000;
System.out.printf(
"Thread %s (ID %d) copied %s → %s in %d ms%n",
threadName, threadId, src, dest, millis
);
} catch (IOException e) {
System.err.printf(
"Thread %s (ID %d) failed to copy %s: %s%n",
threadName, threadId, src, e.getMessage()
);
}
}
}
Обновление:
На основе предложений и комментариев DuncG я придумал следующее:
Semaphore semaphore = new Semaphore(maxThreads);
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (Path file : files) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
executor.submit(() -> {
try {
copyFileTask(file, source, target);
} finally {
semaphore.release();
}
});
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79864787/not-able-to-control-number-of-virtual-threads[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия