Goldman Sachs Interview Question for Software Engineer / Developers






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

threadGroup

- chonin.wu December 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ExecutorService Class

- Arjun June 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public interface ExecutorService extends Executor

An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.

The following example includes two classes. PrintTask.java creates threads that sleep for a random time (0-5 secs). RunnableTester.java executes the PrintTask threads using a fixed thread pool.

/*
* Runnables are executed by an object of a class that implements the Executor interface.
* This interfaces declares the method execute(Runnable). This method creates a new thread
* and launches it immediately.
*/
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;

public class RunnableTester
{
public static void main(String[] args)
{
// create and name each runnable
PrintTask task1 = new PrintTask("thread1");
PrintTask task2 = new PrintTask("thread2");
PrintTask task3 = new PrintTask("thread3");

System.out.println("Starting threads");

// create ExecutorService to manage threads
ExecutorService threadExecutor = Executors.newFixedThreadPool(3);

// start threads and place in runnable state
threadExecutor.execute(task1); // start task1
threadExecutor.execute(task2); // start task2
threadExecutor.execute(task3); // start task3

threadExecutor.shutdown(); // shutdown worker threads

System.out.println("Threads started, main ends\n");
} // end main
} // end class RunnableTester

import java.util.Random;

class PrintTask implements Runnable
{
private int sleepTime; // random sleep time for thread

private String threadName; // name of thread

private static Random generator = new Random();

// assign name to thread
public PrintTask(String name)
{
threadName = name; // set name of thread

// pick random sleep time between 0 and 5 seconds
sleepTime = generator.nextInt(5000);
} // end PrintTask constructor

{
// TODO Auto-generated constructor stub
}

// method run is the code to be executed by new thread
public void run()
{
try
// put thread to sleep for sleepTime amount of time
{
System.out.printf("%s going to sleep for %d milliseconds.\n",
threadName, sleepTime);

Thread.sleep(sleepTime); // put thread to sleep
} // end try
// if thread interrupted while sleeping, print stack trace
catch (InterruptedException exception)
{
exception.printStackTrace();
} // end catch

// print thread name
System.out.printf("%s done sleeping\n", threadName);
} // end method run
} // end class PrintTask

- Sal July 06, 2010 | 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