I am attempting to insert multiple rows of data into a table through a multiThreaded program in a standard Groovy-Grails application. The application uses standard GORM objects for database interaction. Each thread inserts a different set of rows. I am using ExecutorService and FixedThreadPool in a Grails Service that invokes Runnable Groovy class. The Runnable class queries DB, does a lot of computation and inserts the derived values into a table. I am running into the following exception.
`Error java.lang.NullPointerException
Error at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
Error at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85)
Here's the Runnable class.
#Transactional
class MyRunnable{
def aService
def bService
def run(){
for (i=1; i<n; i++){//n is a finite integer
def x = aService.doSomething()
def y = bService.doSomething(x)
AGormClass.addToBGormClass(y)
}
AGormClass.save flush:true
}
}
Here's Grails Service that initializes thread pool and calls Runnable class.
#Transactional
class SvcClass{
ExecutorService exeSvc = Executors.newFixedThreadPool(2)
MyRunnable runnable = new MyRunnable()
exeSvc.execute(runnable as Runnable)
exeSvc.shutdown()
}
Related
This is a scala code I'm trying to write for processing a function multiple times concurrently with two different parameters. However, I notice that the functions are being executed one by one and not both at same time.
class Method1 extends Thread {
override def run(): Unit ={
println("Hello, Current running is Thread No. " + Thread.currentThread().getName )
Function("Parameter 1")
Function("Parameter 2")
}
}
object Obj extends App {
for (x <- 1 to 4){
val th1 = new Method1()
th1.setName(x.toString)
th1.start()
}
}
val a = Array("Justanormalarray")
Obj.main(a)
How to achieve this? Sorry for being dumb but all tutorials I seen only explains a very basic level that multithreading can be achieved by extending thread class or by using runnable interface, but doesn't seem to clear this up (i.e. how to actually execute multiple stuff at same time)
I am trying to create a kafka consumer in a separate thread which consumes data from kafka topic. For this, I have extended ShutdownableThread abstract class and provided implementation for doWork method. My code is like this -
abstract class MyConsumer(topic: String) extends ShutdownableThread(topic) {
val props: Properties = ???
private val consumer = new KafkaConsumer[String, String](props)
consumer.subscribe(List(topic).asJava)
def process(value: String): Unit // Abstract method defining what to do with each record
override def doWork(): Unit = {
for (record <- consumer.poll(Duration.ofMillis(1000)).asScala)
process(record.value())
}
}
Now in my tests, I create consumer providing implementation of process() method which just mutates a variable and then call start() method of it to start the thread.
var mutVar = "initial_value"
val consumer = new MyConsumer("test_topic") {
override def process(value: String): Unit = mutVar = "updated_value"
}
consumer.start()
assert(mutVar === "updated_value")
The Consumer does consume the message from the kafka but it does not update it before the test finishes and hence the test fails. So, I tried to put the main thread on sleep. But it throws ConcurrentModificationException exception with the message - KafkaConsumer is not safe for multi-threaded access
Any idea what is wrong with my approach ? Thanks in advance.
Had to put main thread to sleep for few seconds to allow consumer to consume message from kafka topic and store it in the mutable variable. Added - Thread.sleep(5000) after starting the consumer.
I am trying the execute the below code in which four threads are running.
Each thread trying to add the one character to mylist. After each run, there should be 4 elements in the list in any order. But, when this code is run in jenkin console, getting each time different number of elements. i.e, [A, B, Null], [C,null,D],[A,B,C,D].
It seem like some synchronization issue due to multi tasking. I have tried adding method add element to list using synchronize keyword but that didn't help.
class Globals {
static def mylist=[]
}
class TestMultiThreadExecution implements Runnable
{
String name;
public TestMultiThreadExecution(String name) {
this.name = name;
}
//#Override
public void run() {
println "${name} Step 1"
Globals.mylist.push("${name}")
}
}
Globals.mylist.clear()
Thread thread1 = new Thread(new TestMultiThreadExecution("A"));
thread1.start();
Thread thread2 = new Thread(new TestMultiThreadExecution("B"));
thread2.start();
Thread thread3 = new Thread(new TestMultiThreadExecution("C"));
thread3.start();
Thread thread4 = new Thread(new TestMultiThreadExecution("D"));
thread4.start();
thread1.join();
thread2.join();
thread3.join();
thread4.join();
println Globals.mylist
It is because, list is being concurrently accessed and modified by 4 threads.
You can fix it by changing list type to CopyOnWriteArrayList as shown below.
So, the only change in the above code is to change list type in the Globals class. Here is the changed code snippet.
import java.util.concurrent.CopyOnWriteArrayList
class Globals {
static def mylist = [] as CopyOnWriteArrayList
}
By the, it would be lot easier in groovy to deal with threads. May be you can refer a sample
I am learning Scala multi-thread programming, and write a simple program through referring a tutorial:
object ThreadSleep extends App {
def thread(body: =>Unit): Thread = {
val t = new Thread {
override def run() = body
}
t.start()
t
}
val t = thread{println("New Therad")}
t.join
}
I can't understand why use {} in new Thread {} statement. I think it should be new Thread or new Thread(). How can I understand this syntax?
This question is not completely duplicated to this one, because the point of my question is about the syntax of "new {}".
This is a shortcut for
new Thread() { ... }
This is called anonymous class and it works just like in JAVA:
You are here creating a new thread, with an overriden run method. This is useful because you don't have to create a special class if you only use it once.
Needs confirmation but you can override, add, redefine every method or attribute you want.
See here for more details: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
By writing new Thread{} your creating an anonymous subclass of Thread where you're overriding the run method. Normally, you'd prefer to create a subclass of Runnable and create a thread with it instead of subclassing Thread
val r = new Runnable{ override def run { body } }
new Thread(r).start
This is usually sematincally more correct, since you'd want to subclass Thread only if you were specializing the Thread class more, for example with an AbortableThread. If you just want to run a task on a thread, the Runnable approach is more adequate.
I have a singleton class whose instance is alive throughout the lifespan of my application. From one of the singelton instance method I am creating instance of a Runnable thread and executing it. Will the runnable thread instance become a candidate for garbage collection after its execution or will it remain in heap forever since my singleton instance is always alive?
Eg: In the example below I create a new instance of MyRunnable class each time MySingleton.getInstance().runMyThread() is called. Will these MyRunnable instances be considered for garbage colletion once done with the run() method is completely executed and over?
public class MySingleton
{
private MySingleton instance = null;
private MySingleton() {}
public static MySingleton getInstance()
{
if(instance == null)
{
instance = new MySingleton();
}
}
public void runMyThread()
{
Thread thread = new Thread(new MyRunnable()); //MyRunnable implements Runnable
thread.start();
}
}
In general an object will be a candidate for garbage collection when
it is no longer in scope and
nothing references it.
In this scenario, assuming nothing is referencing the Thread object after it finishes execution it will become a candidate for garbage collection. In your example, your singleton instances does not contain any references to the Thread object - it calls runMyThread() which creates a new Thread object and starts it. The Thread object's scope is contained to the runMyThread() method.