JP Morgan Interview Question for Java Developers


Team: 100
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 4 vote

-->start() method starts a completely new thread and calls run() method to execute it, hence obeys the basic law of threading.
--> In case of run() method, no new thread it created and method executes on current thread as all other methods do.

- Aman Raj March 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

RUN is like common Method .

IF you call run MEthod Twice

run() isn't a special method. It works just like any other method you declare. If you call it, the main thread (actually, the thread you call it from) will run the code in the run method.

If you call run() twice, you simply execute the code twice. Not parallel, but sequentially.

Thread's start() has a completely different purpose. start() gets a new OS thread and lets it execute the code in the Thread's (overridden) run() method, or if you construct a Thread with a Runnable as the argument, it will let the thread execute the code in that Runnable's run() method.

A Thread models a single thread over time. When it's done running code, it's done. You can't start it again.

- Freak March 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

start() method creates a thread and registers it with a thread scheduler, where thread scheduler deals with prioritization and other thread related functionalities and then calls a run() method. So run() method is simply like any other java method which doesn't have any thread nature until and unless it is called through start() method

- Ramesh Gudiya April 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

start() is the method you call in the parent thread to fork a new thread. The JVM then calls run() on the new thread.

In your code, you should override run() (so that it carries out the task) and (in general) leave start() alone.

- Jay March 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

actually thread is started by the Oprating System ,so if we use start() method to call run() method ,OS start new process(thread) for run method.
if we call call directly run() method , OS does not start new thread and run() method executed in default thread (main thread)

- kundan kumar March 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

run the following program and see output :) It prints out current thread name as main when you call t1.run(); and t2.run() explicitely in main

package threadpool;

public class ExtendThread extends Thread {

	public void run(){
		System.out.println("Thread running = " + Thread.currentThread().getName());
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread t1 = new ExtendThread();
		Thread t2 = new ExtendThread();
		t1.start();
		t2.start();
		
		t1.run();
		t2.run();

	}

}

- kavitha March 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

In Java you can create a thread in two different ways:
1. Inherit from Thread. - to start this thread one calls start()
2. Implement the Runnable interface - to start this thread one calls run()

- Anonymous March 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nevermind. The 'Runnable' thread can be started by calling start() as well. :/

- Anonymous March 17, 2014 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More