simple problems in multi thread in java - multithreading

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.

Related

How to run multiple Runnable in Java threadpool?

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

Context switching not happening between a created thread and main thread

I am trying to run a thread in background with while(true) condition, and not using any join after the thread so that it continue running the main thread. But what I am observing is only while loop is running and it is not switching to main thread. please help me find the issue. This is Groovy code.
public static void main(args) {
Thread.start {
while (true) {
long sleepMillis = 2000
System.out.println("inside async block")
Thread.sleep(sleepMillis)
}
}
//main func code goes here
}
Please give me pointers to the issue.
Here You go:
public class Lol {
public static void main(String[] args) {
def t = new Thread(new Runnable() {
public void run() {
while(true) {
println 'lol'
Thread.sleep(500)
}
}
}).start()
println 'other'
}
}

MS TEst: Method not executed when base class is generic

Not duplicate of: Inherited test class from generic base is ignored in MSTest
In my case, the test classes are in the same namespace/assembly.
When unittesting classes which have a lot in common, I would like to use a base test class with a generic parameter. I have boiled the problem down to the following, where my base test method is not being executed, but ONLY in the generic case.
Non-generic: Base test method is EXECUTED:
[TestClass]
public class DerivedTestClass : BaseUnitTest
{
protected override string ReturnMeSomething(object obj)
{
return "test1" + obj.ToString();
}
[TestMethod]
public void derived_test()
{
// This is executed
}
}
[TestClass]
public abstract class BaseUnitTest
{
[TestMethod]
public void base_test()
{
// This is executed
}
protected abstract string ReturnMeSomething(object obj);
}
Generic: Base test method in generic base class is NOT EXECUTED:
[TestClass]
public class DerivedTestClass : BaseUnitTest<string>
{
protected override string ReturnMeSomething(string s)
{
return "test1" + s;
}
[TestMethod]
public void derived_test()
{
// This is executed
}
}
[TestClass]
public abstract class BaseUnitTest<T>
{
[TestMethod]
public void base_test()
{
// This is NOT executed
}
protected abstract string ReturnMeSomething(T t);
}
Can anyone tell me the reason for this?
After a few days, this suddenly works (!!). If anyone ever experiences this same, odd behavior, please write a comment here. I would suggest anyone to reboot and clean+rebuild everything and try again.

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

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