What is the difference between the terms concurrent and parallel execution?
What is Thread in java?
- All Java programs have at least one thread, known as the main thread, which is created by the JVM at the program’s start, when the main() method is invoked with the main thread.
- Every Java thread is created and controlled by the java.lang.Thread class.
- A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.
- multitasking is a method by which multiple tasks, also known as processes, share common processing resources such as a CPU
- Multitasking refers to the ability of the OS to quickly switch between each computing task to give the impression the different applications are executing multiple actions simultaneously.
- As CPU clock speeds have increased steadily over time, not only do applications run faster, but OSs can switch between applications more quickly. This provides better overall performance
Multicore
- When running on a multicore system, multitasking OSs can truly execute multiple tasks concurrently.
- the multiple computing engines work independently on different tasks.
- For example, on a dual-core system, four applications - such as word processing, e-mail, Web browsing, and antivirus software - can each access a separate processor core at the same time. You can multitask by checking e-mail and typing a letter simultaneously, thus improving overall performance for applications.
Multithreading
- Multithreading is the ability of a program or an operating system to serve more than one user at a time and to manage multiple simultaneous requests without the need to have multiple copies of the programs running within the computer.
- The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program called a thread. Each thread has a separate path of its execution. So this way a single program can perform two or more tasks simultaneously.
- Threads are lightweight processes; they share the same address space. In Multithreaded environment, programs make maximum use of CPU so that the idle time can be kept to minimum.
Difference Between Process and Thread?
Process:
- Program in Execution
- Heavy Weight
- Required Separate address space
- inter process communication is expensive
Thread:
- separate path of execution,one or more thread is called process
- light weight
- share same address space
- Inter thread communication is less expensive
simple server and client program where a client types something in, the server receives it, and then it sends it back to the client.
- there will be two separate projects, a Client project and a Server project.
- We
are going to start with the Server project first. In this we will have
to classes, a Main class (what starts the server and accepts clients)
and a Client class (An object that we will set each client to through
their socke
Part 1
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws IOException
{try
{
final int PORT = 6677;//SET NEW CONSTANT VARIABLE: PORT
ServerSocket server = new ServerSocket(PORT); //SET PORT NUMBER
System.out.println("Waiting for clients...");//AT THE START PRINT THIS
while (true)//WHILE THE PROGRAM IS RUNNING
{
Socket s = server.accept();//ACCEPT SOCKETS(CLIENTS) TRYING TO CONNECT
System.out.println("Client connected from " + s.getLocalAddress().getHostName()); // TELL THEM THAT THE CLIENT CONNECTED
Client chat = new Client(s);//CREATE A NEW CLIENT OBJECT
Thread t = new Thread(chat);//MAKE A NEW THREAD
t.start();//START THE THREAD
}
}
catch (Exception e)
{
System.out.println("An error occured.");//IF AN ERROR OCCURED THEN PRINT IT
e.printStackTrace();
}
}
}
final int PORT = 6677;
ServerSocket server = new ServerSocket(PORT);
System.out.println("Waiting for clients...");
- What this does is first it creates a variable port and sets it to our port number.
- Then we make a new ServerSocket and bind it to our port variable.
- Then finally we print out that we are waiting clients.
we have is a while loop which is practically saying while the program is running, do everything inside of it.Then we have the main part which waits for a socket (client) to connect and when one does, it creates a new Client object with the parameter of the Socket
Socket s = server.accept();
System.out.println("Client connected from " + s.getLocalAddress().getHostName());
Client chat = new Client(s);
Thread t = new Thread(chat);
t.start();
- the first line we wait for a socket to connect and when one does we set it to the variable s.
- Then we print out the address they connected from just to let the server owner know when someone connects
- After that we create our new Client object, which you will see soon, and then we create a new thread on the next like. Then we start the thread.
Now we are going to go to the Client.java file where all of the good stuff happens.import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client implements Runnable{
private Socket socket;//SOCKET INSTANCE VARIABLE
public Client(Socket s)
{
socket = s;//INSTANTIATE THE SOCKET
}
@Override
public void run() //(IMPLEMENTED FROM THE RUNNABLE INTERFACE)
{
try //HAVE TO HAVE THIS FOR THE in AND out VARIABLES
{
Scanner in = new Scanner(socket.getInputStream());//GET THE SOCKETS INPUT STREAM (THE STREAM THAT YOU WILL GET WHAT THEY TYPE FROM)
PrintWriter out = new PrintWriter(socket.getOutputStream());//GET THE SOCKETS OUTPUT STREAM (THE STREAM YOU WILL SEND INFORMATION TO THEM FROM)
while (true)//WHILE THE PROGRAM IS RUNNING
{
if (in.hasNext())
{
String input = in.nextLine();//IF THERE IS INPUT THEN MAKE A NEW VARIABLE input AND READ WHAT THEY TYPED
System.out.println("Client Said: " + input);//PRINT IT OUT TO THE SCREEN
out.println("You Said: " + input);//RESEND IT TO THE CLIENT
out.flush();//FLUSH THE STREAM
}
}
}
catch (Exception e)
{
e.printStackTrace();//MOST LIKELY THERE WONT BE AN ERROR BUT ITS GOOD TO CATCH
}
}
}
Part 2
So now is the start of part 2 and the Client project has the same two classes as the server, Main.java and Client.java. So first im going to start with theimport java.io.IOException;
import java.net.Socket;
public class Main {
private final static int PORT = 6677;//SET A CONSTANT VARIABLE PORT
private final static String HOST = "localhost";//SET A CONSTANT VARIABLE HOST
public static void main(String[] args) throws IOException
{
try
{
Socket s = new Socket(HOST, PORT);//CONNECT TO THE SERVER
System.out.println("You connected to " + HOST);//IF CONNECTED THEN PRINT IT OUT
Client client = new Client(s);//START NEW CLIENT OBJECT
Thread t = new Thread(client);//INITIATE NEW THREAD
t.start();//START THREAD
}
catch (Exception noServer)//IF DIDNT CONNECT PRINT THAT THEY DIDNT
{
System.out.println("The server might not be up at this time.");
System.out.println("Please try again later.");
}
}
}
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client implements Runnable {
private Socket socket;//MAKE SOCKET INSTANCE VARIABLE
public Client(Socket s)
{
socket = s;//INSTANTIATE THE INSTANCE VARIABLE
}
@Override
public void run()//INHERIT THE RUN METHOD FROM THE Runnable INTERFACE
{
try
{
Scanner chat = new Scanner(System.in);//GET THE INPUT FROM THE CMD
Scanner in = new Scanner(socket.getInputStream());//GET THE CLIENTS INPUT STREAM (USED TO READ DATA SENT FROM THE SERVER)
PrintWriter out = new PrintWriter(socket.getOutputStream());//GET THE CLIENTS OUTPUT STREAM (USED TO SEND DATA TO THE SERVER)
while (true)//WHILE THE PROGRAM IS RUNNING
{
String input = chat.nextLine();
//SET NEW VARIABLE input TO THE VALUE OF WHAT THE CLIENT TYPED IN
out.println(input);//SEND IT TO THE SERVER
out.flush();//FLUSH THE STREAM
if(in.hasNext())//IF THE SERVER SENT US SOMETHING
System.out.println(in.nextLine());//PRINT IT OUT
}
}
catch (Exception e)
{
e.printStackTrace();//MOST LIKELY WONT BE AN ERROR, GOOD PRACTICE TO CATCH THOUGH
}
}
}