根据这个问题,我们可以修改源,它不称为干扰:
您可以修改流元素本身,不应将其称为"干扰".
根据这个问题,代码
List<String> list = new ArrayList<>();
list.add("test");
list.forEach(x -> list.add(x));
Run Code Online (Sandbox Code Playgroud)
会扔ConcurrentModificationException
.
但我的代码,
Employee[] arrayOfEmps = {
new Employee(1, "Jeff Bezos"),
new Employee(2, "Bill Gates"),
new Employee(3, "hendry cavilg"),
new Employee(4, "mark cuban"),
new Employee(5, "zoe"),
new Employee(6, "billl clinton"),
new Employee(7, "ariana") ,
new Employee(8, "cathre"),
new Employee(9, "hostile"),
new Employee(10, "verner"),
};
Employee el=new Employee(1, "Jeff Bezos");
List<Employee> li=Arrays.asList(arrayOfEmps);
li.stream().map(s->{s.setName("newname");return s;}).forEach(System.out::print);
Run Code Online (Sandbox Code Playgroud)
ConcurrentModificationException
即使它实际上改变了源,也不会抛出.
而这段代码,
Employee[] arrayOfEmps = {
new Employee(1, "Jeff …
Run Code Online (Sandbox Code Playgroud) public class WrapperClasses{
void overloadedMethod(Number N){
System.out.println("Number Class Type");
}
void overloadedMethod(Double D){
System.out.println("Double Wrapper Class Type");
}
void overloadedMethod(Long L){
System.out.println("Long Wrapper Class Type");
}
public static void main(String[] args){
int i = 21;
WrapperClasses wr = new WrapperClasses();
//wr.overloadedMethod(i);
}
}
class mine extends WrapperClasses{
void overloadedMethod(int N){
System.out.println("Integer Class Type");
}
public static void main(String[] args){
int i = 21;
WrapperClasses wr = new mine();
wr.overloadedMethod(i);
}
}
Run Code Online (Sandbox Code Playgroud)
这打印Number Class Type
.
我理解包装类方法重载的规则:
我知道不应该调用run方法来启动一个新的线程执行,但是我在引用这篇文章时他们调用了runnable.run();
另一个run方法,它似乎意味着它启动了一个新线程或者它根本没有创建线程,它只是创建一个新线程并在同一个线程中运行所有runnable,即任务任务?
这是文章引用的代码.
public class ThreadPool {
private BlockingQueue taskQueue = null;
private List<PoolThread> threads = new ArrayList<PoolThread>();
private boolean isStopped = false;
public ThreadPool(int noOfThreads, int maxNoOfTasks){
taskQueue = new BlockingQueue(maxNoOfTasks);
for(int i=0; i<noOfThreads; i++){
threads.add(new PoolThread(taskQueue));
}
for(PoolThread thread : threads){
thread.start();
}
}
public synchronized void execute(Runnable task) throws Exception{
if(this.isStopped) throw
new IllegalStateException("ThreadPool is stopped");
this.taskQueue.enqueue(task);
}
public synchronized void stop(){
this.isStopped = true;
for(PoolThread thread : threads){
thread.doStop();
}
} …
Run Code Online (Sandbox Code Playgroud) 我正在阅读关于java流API和我从这里遇到以下声明
无论流是以串行还是并行方式执行,forOachOrdered操作都按流指定的顺序处理元素.但是,当并行执行流时,映射操作会处理由Java运行时和编译器指定的流的元素.因此,lambda表达式的顺序为e - > {parallelStorage.add(e); 返回e; 向List添加元素parallelStorage每次运行代码时都会有所不同.对于确定性和可预测的结果,请确保流操作中的lambda表达式参数不具有状态.
我测试了我的以下代码,它实际上如上所述.
public class mapordering {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> serialStorage = new ArrayList<>();
System.out.println("Serial stream:");
int j=0;
List<String> listOfIntegers = new ArrayList() ;
for(int i=0;i<10;i++)listOfIntegers.add(String.valueOf(i));
listOfIntegers.stream().parallel() .map(e -> { serialStorage.add(e.concat(String.valueOf(j))); return e; }).forEachOrdered(k->System.out.println(k));;
/*
// Don't do this! It uses a stateful lambda expression.
.map(e -> { serialStorage.add(e); return e; })*/
for(String s:serialStorage)System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
产量
串行流:0 1 2 3 4 5 6 …
报告,IMMUTABLE或CONCURRENT的分裂器保证永远不会抛出ConcurrentModificationException.当然,CONCURRENT在语义上排除了SIZED,但这对客户端代码没有任何影响.
实际上,这些特性并不用于Stream API中的任何内容,因此,使用它们不一致的地方永远不会被注意到.
这也解释了为什么每个中间操作都具有清除CONCURRENT,IMMUTABLE和NONNULL特性的效果:Stream实现不使用它们,并且表示流状态的内部类不维护它们.
如果流不使用来自源的CHARACTERISTICS,那么流如何并行工作?流是否完全忽略了流源特征?
从这个问题 收集者不知道即时通讯使用供应商提供的并发收集,因此特征不受收集器容器类型的影响
根据我的理解,通常应该使用类的引用来调用静态方法,或者如果它在静态方法或静态块中,则可以在没有引用的情况下直接调用它.
但是,当从子类静态块调用静态方法时,这是否适用?
为什么它允许这样的东西,因为静态方法不是继承的,它应该只允许使用父类名吗?
public abstract class abs {
/**
* @param args
*/
abstract void m();
static void n(){
System.out.println("satic method");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class myclass extends abs{
@Override
void m() {
// TODO Auto-generated method stub
}
static{
n();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我的子类静态块可以调用没有引用或类名的父类静态方法?
然而,假设此归约中使用的结果容器是一个可并发修改的集合——例如 ConcurrentHashMap。在这种情况下,累加器的并行调用实际上可以同时将它们的结果存入同一个共享结果容器中,从而无需组合器合并不同的结果容器。这可能会提高并行执行性能。我们称之为并发减少。
还
支持并发减少的收集器标记有 Collector.Characteristics.CONCURRENT 特性。但是,并发收集也有缺点。如果多个线程同时将结果存放到共享容器中,则存放结果的顺序是不确定的。
从文件
这意味着带有供应商(并发线程安全)的 collect 方法应该有Collector.Characteristics.CONCURRENT。因此不应维持任何秩序。
但我的代码
List<Employee> li=Arrays.asList(Employee.emparr());
System.out.println("printing concurrent result "+li.stream().parallel().unordered().map(s->s.getName()).collect(() -> new ConcurrentLinkedQueue<>(),
(c, e) -> c.add(e.toString()),
(c1, c2) -> c1.addAll(c2))
.toString());
Run Code Online (Sandbox Code Playgroud)
总是按照遇到的顺序打印结果。这是否意味着我的Collector.Characteristics 不是 CONCURRENT?如何检查和设置这个特性?