0% found this document useful (0 votes)
4 views

java unit 3 and 4

The document covers fundamental concepts of Input/Output in Java, including byte and character streams, file handling, and multi-threaded programming. It explains the life cycle of threads, advantages and disadvantages of using threads, and methods for creating threads. Additionally, it discusses Swing components, layout managers, and JDBC architecture for database connectivity.

Uploaded by

amitagoel0408
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java unit 3 and 4

The document covers fundamental concepts of Input/Output in Java, including byte and character streams, file handling, and multi-threaded programming. It explains the life cycle of threads, advantages and disadvantages of using threads, and methods for creating threads. Additionally, it discusses Swing components, layout managers, and JDBC architecture for database connectivity.

Uploaded by

amitagoel0408
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Elementary concepts of Input/Output, using the byte streams, reading and writing using byte streams,

automatically closing a file, using the character-based streams, File I/O using character streams (using a File
Writer and using a File Reader)
Multi-threaded programming: Multithreading fundamentals, Thread class, and Runnable interface, the
life cycle of thread, creation of single and multiple threads, implementation of Thread methods,
Synchronization (using Synchronized methods, synchronized statement).
UNIT – IV
No. of Hours: 10 Chapter/Book Reference: TB1 [Chapters – 17, 18]
Swings Fundamentals: Components (JLabel and ImageIcon, using swing Buttons (JButton, JToggleButton,
JCheckBox, JRadioButton), JTextField, JScrollPane, JList, JComboBox) and Containers, Layout managers,
event delegation Model, event handling (event sources, event listeners, event classes and interfaces, adapter
classes).
JDBC: JDBC Architecture, JDBC Drivers, Connection, Statement, Prepared Statement, Result set, Connecting to
the Database using JDBC.
Layout manager
The LayoutManagers are used to arrange components in a particular manner.
The Java LayoutManagers facilitates us to control the positioning and size of
the components in GUI forms.
LayoutManager is an interface that is implemented by all the classes of layout
managers.

FlowLayout

The Java FlowLayout class is used to arrange the components in a line,


one after another (in a flow). It is the default layout of the applet or panel.

BorderLayout
The BorderLayout is used to arrange the components in five regions:
north, south, east, west, and center. Each region (area) may contain one
component only. It is the default layout of a frame or window.

GridLayout

The Java GridLayout class is used to arrange the components in a


rectangular grid. One component is displayed in each rectangle.
q. diff between byte code and machine code

When we compile java program,Byte code is Machine code is essentially machine


generated language that the CPU can process directly.
Its in hexa decimal format It's in binary format
it cannot be directly run by the CPU and It doesn't require separate interpretation or
requires an interpreter for execution.e.g jvm compilation.

q. what is an adapter class

Adapter Class is a simple java class that implements an interface


with only an empty implementation.
Benefits of Using Adapter Classes in Java

 Simplified implementation of interfaces


 Improved code reusability
 Reduced code duplication
 Easier maintenance and refactoring of code
 Increased flexibility in code design

e.g MouseAdapter Class

q.Advantages & Disadvantages of threads

Advantages of threads
1. We can execute multiple tasks of an application at a time
2. Reduces the complexity of a big applications
3. Helps to improve the performance of an application drastically
4. Utilizes the max resources of multiprocessor systems
5. Better user interface in case of GUI based applications
6. Reduces the development time of an application
7. All the threads are independent , any unexpected exception happens in
any of the thread will not lead to an application exit.

Disadvantages of threads
1. Thread synchronization is an extra over head to the developers
2. Shares the common data across the threads might cause the data
inconsistency or thread sync issues
3. Threads blocking for resources is more common problem
4. Difficult in managing the code in terms of debugging or writing the
code
q.complete life cycle of a thread

The following diagram shows the complete life cycle of a thread.

New − A new thread begins its life cycle in the new state. It remains in this state until the program
starts the thread. It is also referred to as a born thread.

Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state
is considered to be executing its task.

Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another
thread to perform a task. A thread transitions back to the runnable state only when another thread
signals the waiting thread to continue executing.

Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time.
A thread in this state transitions back to the runnable state when that time interval expires or when
the event it is waiting for occurs.

Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or
otherwise terminates.

q.There are two different ways to create a thread in Java.


 By Extending a Thread Class
 By Implementing a Runnable Interface
By Extending a Thread Class

1. class Testthread extends Thread{


2. public void run(){
3. System.out.println("thread is running...");
4. }
5. public static void main(String args[]){
6. Testthread t1=new Testthread ();
7. t1.start();
8. }
9. }

Java Thread Example by implementing Runnable interface

1. class Testthread implements Runnable{


2. public void run(){
3. System.out.println("thread is running...");
4. }
5.
6. public static void main(String args[]){
7. Testthread m1=new Testthread ();
8. Thread t1 =new Thread(m1); // Using the constructor Thread(Runn
able r)
9. t1.start();
10. }
11. }

Some methods of thread class

isAlive() Tests if this thread is alive

getName() Returns this thread’s name

getPriority Returns this thread’s


() priority

join() Waits for this thread to die


Returns the state of this
getState()
thread

What is Daemon Thread in Java?

 A daemon thread is a thread that runs in the background


 does not prevent the Java Virtual Machine (JVM) from exiting when all non-
daemon threads in Java have been completed.
 These threads are typically used to perform background tasks such as
garbage collection, monitoring, and other system-level operations.
 Daemon threads are created using the `setDaemon(true)` method of the
`Thread` class.

A stream can be defined as a sequence of data. There are two


kinds of Streams −

 InputStream − The InputStream is used to read data from


a source.
 OututStream − The OutputStream is used for writing data
to a destination.

Byte stream is used to read and write a single byte (8 bits) of data.

All byte stream classes are derived from base abstract classes
called InputStream and OutputStream .

Character stream is used to read and write a single character of data.

All the character stream classes are derived from base abstract
classes Reader and Writer .

You might also like