Skip to content

Commit

Permalink
Remove jobs from on demand job scheduler by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko authored and martinfurmanski committed Mar 13, 2017
1 parent 59a482c commit 540cce9
Showing 1 changed file with 19 additions and 8 deletions.
Expand Up @@ -19,9 +19,9 @@
*/
package org.neo4j.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;
Expand All @@ -32,12 +32,21 @@

import static org.neo4j.kernel.impl.util.JobScheduler.Group.NO_METADATA;

/**
* This class is far from perfect when it comes to managing multiple jobs.
*/
public class OnDemandJobScheduler extends LifecycleAdapter implements JobScheduler
{
private List<Runnable> jobs = new ArrayList<>();
private List<Runnable> jobs = new CopyOnWriteArrayList<>();

private final boolean removeJobsAfterExecution;

public OnDemandJobScheduler()
{
this( true );
}

public OnDemandJobScheduler( boolean removeJobsAfterExecution)
{
this.removeJobsAfterExecution = removeJobsAfterExecution;
}

@Override
public Executor executor( Group group )
Expand Down Expand Up @@ -100,11 +109,13 @@ public Runnable getJob()

public void runJob()
{
/* some tests modify the scheduler concurrently */
Runnable[] copy = this.jobs.toArray( new Runnable[this.jobs.size()] );
for ( Runnable job : copy )
for ( Runnable job : jobs )
{
job.run();
if ( removeJobsAfterExecution )
{
jobs.remove( job );
}
}
}

Expand Down

0 comments on commit 540cce9

Please sign in to comment.