Tag: multithreading

如何在任何一个线程完成后终止所有其他正在运行的线程

问题:我有一个并行开始循环的线程集合。 首先退出任何线程后,必须终止所有其他正在运行的线程。 这是我尝试过但它不起作用。 任何帮助表示赞赏。 public class ThreadsMain { public static void main(String[] args) { int SIZE = 3; Thread t[] = new Thread[SIZE]; for (int i = 0; i < SIZE; i++) { myThreads th = new myThreads(); t[i] = new Thread(th); t[i].start(); } } }

线程安全LinkedHashMap没有Collections.synchronized

我正在使用LinkedHashMap,并且环境是multithreading的,因此这个结构需要是线程安全的。 在特定事件期间,我需要读取整个地图推送到数据库并清除所有。 大多数时候只有写入发生在这张地图上。 此地图限制了50个条目。 我使用的是Oracle MAF,它没有Collections.syncronizedMap。 那么,我需要在synchronized块中放置什么东西以确保写入和读取不会遇到concurrentModificationException等 几个要求: 我需要像循环队列一样表现它,以便重写LinkedHashMap的removeEldestEntry方法。 我需要保留订单

Java线程和MySQL

我有一个线程聊天服务器应用程序,需要MySQLvalidation。 让1个类创建MySQL连接,保持该连接打开并让每个线程使用该连接但使用自己的Query处理程序是最好的方法吗? 或者让所有线程与MySQL分开连接以进行身份​​validation更好? 或者让1个类处理查询和连接更好吗? 我们正在寻找一个应该能够处理多达10.000个连接/用户的聊天服务器。 我现在正在使用c3p0,我创建了这个: public static void main(String[] args) throws PropertyVetoException { ComboPooledDataSource pool = new ComboPooledDataSource(); pool.setDriverClass(“com.mysql.jdbc.Driver”); pool.setJdbcUrl(“jdbc:mysql://localhost:3306/db”); pool.setUser(“root”); pool.setPassword(“pw”); pool.setMaxPoolSize(100); pool.setMinPoolSize(10); Database database = new Database(pool); try { ResultSet rs = database.query(“SELECT * FROM `users`”); while (rs.next()) { System.out.println(rs.getString(“userid”)); System.out.println(rs.getString(“username”)); } } catch(Exception ex) { System.out.println(ex.getMessage()); } finally { database.close(); } […]

并发程序降级的性能随着线程的增加而增加?

我一直在尝试在四核计算机上实现以下代码,并且Executor服务中100多次迭代中没有线程的平均运行时间如下 1个线程= 78404.95 2个主题= 174995.14 4个线程= 144230.23 但根据我所研究的, 2*(no of cores)线程应该为程序提供最佳结果,这在我的程序中显然不是这样,奇怪地给出了单线程的最佳时间。 代码: import java.util.Collections; import java.util.Random; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class TestHashSet { public static void main(String argv[]){ Set S = Collections.newSetFromMap(new ConcurrentHashMap()); S.add(1); S.add(2); S.add(3); S.add(4); S.add(5); long startTime = System.nanoTime(); ExecutorService executor = Executors.newFixedThreadPool(8); int Nb […]

JavamultithreadingWeb服务器 – 未收到多个GET请求

我有一个非常基本的multithreadingWeb服务器的启动,它可以接收所有GET请求,只要它们一次来一个。 但是,当多个GET请求同时进入时,有时它们都会被收到,有时则会丢失一些。 我通过创建一个html页面测试了这一点,该页面包含指向我的web服务器并在firefox中打开页面的多个图像标记。 我总是使用shift + refresh。 这是我的代码,我必须做一些根本错误的事情。 public final class WebServer { public static void main(String argv[]) throws Exception { int port = 6789; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); } catch(IOException e) { System.err.println(“Could not listen on port: ” + port); System.exit(1); } while(true) { try { Socket clientSocket = […]

在thread.start之前发生的所有事情都可以生成调用start的线程吗?

现在已经有很好的答案已经在stackoverflow上,但他们没有给我我想要的明确答案。 假设你有一个方法 Dosomething(); doAnother(); int x = 5; Runnable r = new Runnable(){ public void run(){ int y = x; x = 7; System.out.println(z);} } new Thread(r).start() 现在同时这个方法正在运行,在调用thread.start之前,一些全局非易失性变量z从4变为5。 由于z发生在thread.start之前,程序是否会保证打印5? 另外,如果我们以这种方式讨论它,那么可以肯定地说thread.start()永远不会被重新排序。 根据被调用的那个线程的意义开始,就好像到那一点的一切都是顺序的。 比如说我们有 int k = 8; new Thread(() -> {}).start() 现在……在该线程的视角中,无论是首先调用start还是分配k,都不会产生影响。所以这可以重新排序,但是因为在保证之前发生了,这是不可能的? java规范没有说明这一点的强烈声明。 相反它说 当一个语句调用Thread.start()时,每个语句都与该语句有一个before-before关系 然而,k = 8并不是在与该陈述的关系之前发生的…… 我甚至不确定他们的意思是在与start方法的关系之前发生了什么事情,除非您使用相同的监视器锁定 synchronized(this){ int k = 8;} synchronized(this) { […]

unit testing框架 – 使用可配置值定义threadPoolSize的TestNG

我正在挖掘一下TestNG框架。 我在我的测试用例中使用注释来配置线程值,例如: @Test(threadPoolSize = 2, invocationCount = 10) public void testOne() { //some code } 这个想法是在配置文件中配置这些值,这些值应该传递给所有测试。 所以我需要从配置项中更改这些值或通过unitTest构造函数传递此值,但TestNG只接受CONSTANT值。 任何提示/想法? 提前致谢!

每种方法或每个类都会发生死锁吗?

想象一下,我有几个同步方法的Class First 。 当一个线程锁定类First ,它是按每个方法还是按类锁定的? 例如,以下代码是否会发生死锁? public class DeadLockQuestion { public static class First{ public synchronized void a(){ } public synchronized void b(){ } public synchronized void c(){ } public synchronized void d(){ } public synchronized void e(){ } } public static void main(String… args){ First f = new First(); //This code is run in […]

简单的GUI倒计时应该如何工作?

我正在尝试编写简单的GUI倒计时。 我在互联网上找到了一些代码,但它对我来说已经过于花哨了。 我想尽量保持简单。 所以,我只想要一个窗口说“你剩下10秒钟”。 秒数应该每秒从10减少到0.我写了一个代码。 我认为我接近工作解决方案。 但我仍然遗漏了一些东西。 你能请求帮助我找出问题所在吗? 这是我的代码: import javax.swing.*; public class Countdown { static JLabel label; // Method which defines the appearance of the window. private static void showGUI() { JFrame frame = new JFrame(“Simple Countdown”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel(“Some Text”); frame.add(label); frame.pack(); frame.setVisible(true); } // Define a new thread in […]

为什么线程仍然不一致?

我有一些模拟银行转账的java代码。 帐户类只有一个余额字段和一个向余额字段添加一些余额的转移方法。 TransferManager定义了一个Transfer类,它使用两个Account对象将一个帐户中的给定金额转移到另一个帐户作为参数传递。 Manager本身有两个需要同步的重要方法,因为它们都在同一个资源上运行,并且它们将以线程方式调用: public synchronized void issueTransfer(Account from, Account to, int amount) { openTransfers.add(new Transfer(from, to, amount)); issuedTransfers++; } public synchronized void performTransfers() { for(Transfer transaction : openTransfers) { transaction.performTransfer(); performedTransfers++; } openTransfers.clear(); } 如果没有同步语句,我会在存储和读取传输的arraylist上获得NullPointerExceptions。 BankTest产生10个线程,每个线程发出10次传输。 看看BankTest.java吧。 问题是并不总是发出10 * 10次转账。 有时有98或99: 我是否必须向BankTest.java添加同步? 我该怎么办? 还有其他想法或建议吗? TransferManager.java:http://pastebin.com/Je4ExhUz BankTest.java:http://pastebin.com/cdpWhHPb Exersice3.java:http://pastebin.com/v7pwJ5T1 Account.java:http://pastebin.com/QYEeWy5Z