0

I am a newbie to Java and wondering whether I can create threads in following way.

Desired Java Code :

Class MyClass {

    Myclass(){
        Statement1;//Create a thread1 to call a function
        Statement2;//Create a thread2 to call a function
        Statement3;//Create a thread3 to call a function
    }
}

Is it possible to create threads like the above code?

4 Answers 4

3

The Java Concurrency tutorial includes a page on defining and starting threads. You might want to read through it along with the other pages in the concurrency tutorial.

Sign up to request clarification or add additional context in comments.

2 Comments

In particular, the section "High Level Concurrency Objects" may be valuable. Frequently the higher-level objects are simpler to use.
can't quite understand from any of them. Created an empty runnable class and trying to call 2 different functions from different classes. Eclipse doesn't even allow me to write the class
1

Echoing GregInYEG, you should check out the tutorial, but the simple explanation is as follows:

You need to create an object class which either extends Thread or implements Runnable. In this class, create (actually, overload) a void method called "run." Inside this method is where you put the code that you would like this thread to execute once it is forked. It could simply be a call to another function if you wish. Then, when you would like to spawn a thread of this type, create one of these objects and call the "start" (not run!) method of this object. eg newThread.start();

It's important to call "start" and not "run" because a run call will simply call the method just like any other, without forking a new thread.

Still, be sure to read up in further detail and there are many more important aspects of concurrency, especially that of locking shared resources.

Comments

0

Yes, it is possible. You want to put your logic for each statement inside a Runnable implementation, and then pass each constructed Runnable to a new instance of Thread. Check out those 2 classes and it should become fairly obvious what you need to do.

Comments

0

I agree with all written here. The thread can be created in a two ways.

  1. To extend thread class . YouTube Tutorial
  2. To implement Runnable Interface YouTube Tutorial

Example for the first method

public class MyThread extends Thread {


public void run()
{
    int iterations = 4;


        for(int i=0;i<iterations;i++)
        {

            System.out.println("Created Thread is running " + Thread.currentThread().getId()  + " Printing "  + i) ;
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.err.println(e);
            }
        }


    System.out.println("End of program"); 
}

}

To create a thread

MyThread myThread = new MyThread();

myThread.start();

Second method to implement runnable interface

public class RunnableThread implements Runnable {

@Override
public void run() {

    int iterations = 4;


    for(int i=0;i<iterations;i++)
    {

        System.out.println("Runnable Thread is running " + Thread.currentThread().getId()  + " Printing "  + i) ;
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.err.println(e);
        }
    }


System.out.println("End of program"); 
}

}

To create a thread

new Thread(new RunnableThread()).start();

So I think you can use both of these methods in you case statements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.