我的主要内容下面有以下代码:
System.out.println(Thread.currentThread().getId());
for(inti=0;i!=Lock.totalThreads;i++) {
System.out.println("thread wascreated");
(new Thread(new MyThread())).start();
}
System.out.println("main finished running files");
Run Code Online (Sandbox Code Playgroud)
MyThread类看起来像这样:
public class MyThread implements Runnable {
private static int threadCounter=0;
private int myThreadId=0;
@Override
public void run() {
synchronized(Lock.lock){
threadCounter++;
myThreadId=threadCounter;
}
System.out.println("run()");
try {
runMe();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void runMe() throws IOException, InterruptedException {
String currentFile;
BufferedReader in;
System.out.println("run()"); …Run Code Online (Sandbox Code Playgroud) 我有一个静态方法
public static void abc(String str) {
// some code
str = str + "s";
// some code
}
Run Code Online (Sandbox Code Playgroud)
让我们假设这个方法由100个线程同时调用.
我认为CPU会调度所有这些线程来执行这个静态方法.
允许扩展执行请求.现在,大约有100000个线程同时调用这个静态方法.
如果是这样,那将是性能开销(与此方法是类的成员的情况相比).我对么?
我正在使用ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(20000);
我在ThreadSystem.java类中有两个静态成员:
public static Integer count = 0;
public static Integer rejectedCount = 0;
Run Code Online (Sandbox Code Playgroud)
然后我正在添加线程:
for (i = 0; i < 20001; i++) {
Runnable worker = new MyThread();
try {
executor.execute(worker);
} catch (RejectedExecutionException ex) {
rejectedCount++;
}
}
executor.shutdown();
while (!executor.isTerminated()) {
}
Run Code Online (Sandbox Code Playgroud)
在线程内:
@Override
public void run() {
ThreadSystem.count++;
try{
Thread.sleep(50);
}
catch(InterruptedException ex){
Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, ex.getMessage(),ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果表明存在未执行的线程且count变量不等于创建的线程数,尽管rejectedCount引用被拒绝的线程为0:
数:19488
拒绝计数:0
那么还有什么可以向我保证所有线程都会运行,这种情况的原因是什么:count(可运行线程)不等于添加的线程?
java multithreading threadpool threadpoolexecutor java-threads
在Java中使用synchronized关键字全部在单个对象和线程中.
我可以调用一个调用非同步方法的同步方法来调用同步方法,而不会阻止第一个同步方法完成的最终同步方法吗?
我只需要用java线程做一个竞争条件的例子,我写了这个代码,但我不确定它是否有竞争条件.
有人可以告诉我下面的代码是否有竞争条件,还有我如何改进或简化?
(抱歉英文不好)
public class RaceCondition extends Thread{
static int count=0; //the variable where the race condition need to happen
static int contador1 = 0; //variables to count
static int contador2 = 0;
static Thread t1 = new Thread(new Runnable() {
public void run(){
while(contador1!=20){
count ++;
System.out.print(count + " ");
contador1++;
}
}
});
static Thread t2 = new Thread(new Runnable() {
public void run(){
while(contador2!=20){
int k = 5;
count += 5*k;
System.out.print(count + " ");
contador2 ++; …Run Code Online (Sandbox Code Playgroud) 为什么在同一类锁上等待并通知函数不能正常工作?
请参阅以下检查代码,了解等待和通知功能及其输出.
输出:
Thread-1
Thread-2
Thread-2 after notify
Run Code Online (Sandbox Code Playgroud)
预期结果:
Thread-1
Thread-2
Thread-2 after notify
Thread-1 after wait
Run Code Online (Sandbox Code Playgroud)
码:
public class WaitAndNotify1 {
public static void main(String[] args) {
Thread t1=new Thread(new Runnable(){
@Override
public void run(){
System.out.println("Thread-1");
try {
synchronized (this) {
wait();
System.out.println("Thread-1 after wait");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2=new Thread(new Runnable(){
@Override
public void run(){
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread-2");
synchronized (this) {
notify();
System.out.println("Thread-2 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Java 套接字开发客户端-服务器应用程序。
客户端必须等待服务器的响应。当服务器收到客户端的请求时,它会执行某些任务以获得必须传递给客户端的结果 - 但执行这些任务可能会导致错误。
有什么方法可以让应用程序在发生此错误时不会终止并可以继续处理请求吗?
package Socket;
import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server
{
public static void main(String[] args)
{
MainWindow application=new MainWindow();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MainWindow extends JFrame implements Runnable
{
public MainWindow()
{
setBounds(1200,300,280,350);
JPanel myPanel= new JPanel();
myPanel.setLayout(new BorderLayout());
text=new JTextArea();
myPanel.add(text,BorderLayout.CENTER);
add(myPanel);
setVisible(true);
Thread t= new Thread(this);
t.start();
}
private JTextArea text;
@Override
public void run()
{
try
{
ServerSocket …Run Code Online (Sandbox Code Playgroud) 我正在尝试下面的代码,我中断了一个用户线程,当我打印 isInterrupted 的值时,它返回 false,我没有得到一个 true 值,在这里,当捕获异常时,标志将被重置或关于调用被中断的方法。
其次,根据我的理解,sleep方法应该在每次迭代中抛出和interruptedException,catch打印中的值,但它只抛出一次。
class ThreadInterruptt extends Thread
{
public void run()
{
for(int i = 0; i<100;i++)
{
try {
Thread.sleep(1000);
System.out.println(i);
System.out.println(isInterrupted());
} catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
}
}
}
public class ThreadInterrupt {
public static void main(String ag[]) throws InterruptedException
{
ThreadInterruptt t = new ThreadInterruptt();
t.start();
Thread.sleep(100);
t.interrupt();
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,我正在使用该字段,number即使IntStream.range(0, 10).forEach(number -> new Thread(r).start());它没有被使用。有没有办法重写它,以便我不必使用未使用的数字变量?
import javax.annotation.concurrent.NotThreadSafe;
import java.util.stream.IntStream;
public class LearnThreads {
public static void main(String[] args) {
UnsafeThreadClass counter = new UnsafeThreadClass();
Runnable r = () -> {
try {
System.out.println(counter.getValue() + " : Thread - " +Thread.currentThread());
} catch (InterruptedException e) {
e.printStackTrace();
}
};
IntStream.range(0, 10).forEach(number -> new Thread(r).start());
}
}
@NotThreadSafe
class UnsafeThreadClass {
private int value;
public synchronized int getValue() throws InterruptedException {
Thread.sleep(1000);
return value++;
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建在单独的 Java 线程池上运行的虚拟线程池。
这是我试图创建的架构:
这是为了让我能够创建单独的池来在一个 JVM 中运行批处理任务,并利用每个池的 n:m 映射的虚拟线程。因此,如果我有 12 个核心,那么我可以创建 2 个 6 线程的线程池。每个池只会执行一个特定的任务。每个池将有 N 个虚拟线程。因此,这里的映射将是 2 个 {N VirtualThreads -> 6 Platform Threads} 池。
TLDR,我想限制虚拟线程池可以运行的 PlatformThreads 数量。
我能想到的一件事是,创建线程池,当传入可运行对象时,在 run 方法内我可以创建虚拟线程,但不确定它有多实用,以及我是否会得到我想要的池分区。这种方法的另一个问题是,虚拟线程将仅在一个 java 线程中运行,因此没有 N:M 映射
java threadpoolexecutor java-threads virtual-threads java-21
java ×10
java-threads ×10
static ×2
concurrency ×1
exception ×1
java-21 ×1
java-stream ×1
sockets ×1
synchronized ×1
threadpool ×1