Thursday, December 6

Thread Scheduling And Socket

1.      Thread Scheduling
There are two kinds of thread scheduling, preemptive and cooperative.
  • A preemptive thread scheduler determines when a thread has had its fair share of CPU time, pauses that thread, and then hands off control of the CPU to a different thread.
  • A cooperative thread scheduler waits for the running thread to pause itself before handing off control of the CPU to a different thread.
  • A virtual machine that uses cooperative thread scheduling is much more susceptible to thread starvation than a virtual machine that uses preemptive thread scheduling.
2.      Synchronization, wait(), notify(), notifyAll()
Monitor objects maintain a list of all threads waiting to enter the monitor object to execute synchronized methods. A thread is inserted in the list and waits for the object if that thread calls a synchronized method of the object while another thread is already executing in a synchronized method of that object. A thread also is inserted in the list if the thread calls wait while operating inside the object. However, it is important to distinguish between waiting threads that blocked because the monitor was busy and threads that explicitly called wait inside the monitor. Upon completion of a synchronized method, outside threads that blocked because the monitor was busy can proceed to enter the object. Threads that explicitly invoked wait can proceed only when notified via a call by another thread to notify or notifyAll. When it is acceptable for a waiting thread to proceed, the scheduler selects the thread with the highest priority.

A socket is a connection between two hosts. It can perform seven basic operations:

·         Connect to a remote machine
·         Send data
·         Receive data
·         Close a connection
·         Bind to a port
·         Listen for incoming data
·         Accept connections from remote machines on the bound port

Client Sockets
·         The program creates a new socket with a Socket( ) constructor.
·         The socket attempts to connect to the remote host.
·         Once the connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other.
·         This connection is full-duplex; both hosts can send and receive data simultaneously.
·         What the data means depends on the protocol; different commands are sent to an FTP server than to an HTTP server. There will normally be some agreed-upon hand-shaking followed by the transmission of data from one to the other.
·         When the transmission of data is complete, one or both sides close the connection. Some protocols, such as HTTP 1.0, require the connection to be closed after each request is serviced. Others, such as FTP, allow multiple requests to be processed in a single connection.

No comments:

Post a Comment