How to run multiple Runnable in Java threadpool? - multithreading

How can I execute bunch of different method or Runnable using "ExecutorService" parrally?
I think you understood what I am trying to say..
Thanks

An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:
Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the HelloThread example:
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
See java documentation here https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Related

simple problems in multi thread in java

I need help to get the answer for this :
The question asked what is the output of the following codes?
package tryScope;
public class MyThread extends Thread{
public void run() {
System.out.println("MyThread: run()");
}
public void start() {
System.out.println("MyThread: start()");
}
}
class MyRunnable implements Runnable{
public void run() {
System.out.println("MyRunnable: run()");
}
public void start() {
System.out.println("MyRunnable: start()");
}
}
public class myTest {
public static void main(String[] args) {
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
}
}
So, I am suppose to choose 1 correct answer but I can't even make it run. I would like to verify if there is something wrong with the code the question is set?
Here are the 4 choices:
Prints: MyThread: run() followed by MyRunnable: start()
Prints: MyThread: start() followed by MyRunnable:start()
Prints: MyThread: run() followed by MyRunnable:run()
Prints: MyThread: start() followed by MyRunnable:run()
Please let me know which is the correct answer and if there is something wrong with the code.
Tks.
I think this code is just stupid. The output should be "MyThread: start()" and nothing else.
Thats because in the class MyThread the start method is overwritten. So there's no Thread starting at all. You can try modify the method like this:
public class MyThread extends Thread{
public void run() {
System.out.println("MyThread: run()");
}
public void start() {
System.out.println("MyThread: start()");
super.start();
}
}
Then the ouput should be something like "MyThread: start()" followed by "MyThread: run()"
To achieve this:
MyThread: start() followed by MyRunnable:run()
add in the main-method the line:
myRunnable.run()
Note: You shouldn't override the start method in Thread at all, as mentioned above from Hejday. This method handles the intern creation and starting of the Thread. The Thread will then execute it's run method see Thread.

How to keep the stack multithread in it memory space

I want to know about what is the best way to develop a thread pool in java to
do multitask and ,how to stack memory keep it when this sample program executing .
please explain me about what is the best way to implement thread pool?
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
class A implements Runnable {
#Override
public void run() {
System.out.println("A "+Thread.currentThread().getName());
}
}
class B implements Runnable {
#Override
public void run() {
System.out.println("B "+Thread.currentThread().getName());
}
}
class C implements Runnable {
#Override
public void run() {
System.out.println("C "+Thread.currentThread().getName());
}
}
public class threadrunner {
public static void main(String[] args) {
ExecutorService bn = Executors.newFixedThreadPool(2);
Runnable t1 = new A();
Runnable t2 = new B();
Runnable t3 = new C();
bn.execute(t1);
bn.execute(t2);
bn.execute(t3);
bn.shutdwon();
}
}

Setting a label in a thread causes IllegalStateException

I have a thread like:
startButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field arg0, int arg1) {
Thread thread = new Thread(){
public void run() {
uploadFile();
}
};
thread.start();
}
//});
});
The uploadFile method contains the line label_up_result.setText(result); which causes an IllegalStateException.
label_up_result is defined like: final LabelField label_up_result=new LabelField("", LabelField.FIELD_LEFT);
What can be the problem ? How can I fix it ?
The problem is probably that you are trying to update the UI from a worker thread. There are two approaches. You can synchronize on the event lock:
synchronized(UiApplication.getUiApplication().getEventLock())) {
label_up_result.setText(result);
}
or you can create a Runnable to execute on the UI thread:
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
label_up_result.setText(result);
}
});
I don't know about blackberry, but usually you need to perform the ui-actions in the ui-thread. SwingUtilities.invokeLater provides that functionality in JavaSE, http://www.java2s.com/Code/Java/Swing-JFC/Swinginvokelater.htm

Postsharp - Adding OnMethodBoundaryAspect to abstract Method - Aspect Not Firing

I'm trying to implement an OnMethodBoundary aspect on an abstract method in an abstract class so that all types that inherit from this class will automatically have the aspect applied. There are no compilation errors or warnings, but the OnEntry method doesn't fire. Note: If I apply the aspect to a non-abstract method, everything works fine
here's the aspect example:
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method, Inheritance = MulticastInheritance.Multicast)]
public sealed class DoSomethingAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
//Do work
}
}
// here's the abstract class
public abstract class Job
{
//...
[DoSomething]
public abstract void Run();
}
Updated answer: it doesn't matter where anything is, as long as both projects have Postsharp referenced then you're good to go.
It works just fine. Which version of PostSharp are you using?
class Program
{
static void Main(string[] args)
{
Job1 j = new Job1();
j.Run();
Console.ReadKey();
}
}
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method, Inheritance = MulticastInheritance.Multicast)]
public sealed class DoSomethingAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
Console.WriteLine("OnEntry");
}
}
public abstract class Job
{
//...
[DoSomething]
public abstract void Run();
}
public class Job1 : Job
{
public override void Run()
{
Console.WriteLine("Run method");
}
}
Results:
OnEntry
Run method

manage thread with junit4

With Junit4, I tried to write a test (.class) that contains 3 #test and need to open the app in each test.
So in the function init that start the app and close it:
#BeforeClass
public static void setupOnce() {
final Thread thread = new Thread() {
public void run() {
//start the appli in the main
thread.start();
}
}
}
#AfterClass
public static void CloseAppli() {
closeAppli();
}
In my testClass: TestButtons.java I want to start the appli in each #test which is not possible...
Any idea?
It seems what you're looking for is the #After method. That's called after every individual test. #AfterClass is only called once at the ending of ALL the tests.
http://junit.sourceforge.net/javadoc/org/junit/After.html

Resources