java线程间传递信息问题,求大神 线程java 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用LinkedBlockingQueue 来做容器就好了,自带锁。 import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Demo5 { private static List<People> peoples = new ArrayList<People>(); public static void main(String[] args) { for (int i = 0; i < 18; i++) { peoples.add(new People("#" + i + "people")); } Demo5 demo = new Demo5(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(demo.new ViewPeople()); exec.execute(demo.new ViewPeople()); exec.shutdown(); } class ViewPeople implements Runnable { public void run() { try { while (!Thread.interrupted()) { synchronized (peoples) { if (peoples.size() > 0) { People people = peoples.get(0); System.out.println(people); peoples.remove(people); } else { break; } } } } catch (Exception e) { } } }}class People { private static int count; private final int id = count++; private String name; public People(String name) { this.name = name; } public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return id + ": " + name; }} 1、哪个方法里访问list就加锁,访问完移除对象,线程跳出run,线程消亡。2、不要用arraylist,改用线程安全的队列。 这样每次只有一个线程在访问List,我需要2个线程同时可以访问List 这样每次只有一个线程在访问List,我需要2个线程同时可以访问List 这样每次只有一个线程在访问List,我需要2个线程同时可以访问List 一个list对象只有一个对象锁, 怎么同时访问, 除非不同步控制 孩子,同步的概念就是宏观同步,微观不同步,用Victor 就可以了 你是想让B知道A是否在使用P吗?那就用lock。A使用P时尝试获取lock,成功继续操作。B尝试获取lock,失败则P正在被使用。 JAVA运行问题 求一款免费好用的GUI画图工具 请看一下问题在哪? java很基础的问题,大家不要笑话我。我是个新手! 请问Hibernate这个是做什么用的?? 如何获取要下载的文件名?(HttpURLConnection) JComponent作为图形容器,用处和用法是怎么回事? 在做一个记事本程序时遇到的问题, 偶一个程序,一个星期之后内存耗尽,有什么工具可以检测? 有没有这样的软件:把一个不标准格式的JAVA原代码转换为具有标准(或比较标准)编码规范的代码。 求助: Java POI如何输出带函数公式的单元格 本人Java菜鸟问几个关于图形化界面聊天软件的问题,望高手指点迷津!
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class Demo5 {
private static List<People> peoples = new ArrayList<People>(); public static void main(String[] args) {
for (int i = 0; i < 18; i++) {
peoples.add(new People("#" + i + "people"));
} Demo5 demo = new Demo5();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(demo.new ViewPeople());
exec.execute(demo.new ViewPeople());
exec.shutdown();
} class ViewPeople implements Runnable { public void run() {
try {
while (!Thread.interrupted()) {
synchronized (peoples) {
if (peoples.size() > 0) {
People people = peoples.get(0);
System.out.println(people);
peoples.remove(people);
} else {
break;
}
}
}
} catch (Exception e) {
}
} }
}class People {
private static int count;
private final int id = count++;
private String name; public People(String name) {
this.name = name;
} public int getId() {
return id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return id + ": " + name;
}}
2、不要用arraylist,改用线程安全的队列。
这样每次只有一个线程在访问List,我需要2个线程同时可以访问List
这样每次只有一个线程在访问List,我需要2个线程同时可以访问List
这样每次只有一个线程在访问List,我需要2个线程同时可以访问List
一个list对象只有一个对象锁, 怎么同时访问, 除非不同步控制
那就用lock。
A使用P时尝试获取lock,成功继续操作。
B尝试获取lock,失败则P正在被使用。