Computer Notes, Programming codes, Hardware and Networking Tip, Entertainment, Biography, Internet Tip, Tech News, Latest Technology, YouTube,
CLS s$ = "NEPAL" r = 1 t = 10 FOR i = 5 TO 1 STEP -2 PRINT TAB(t); MID$(s$, r, i) r = r + 2 t = t + 2 NEXT i END
DECLARE SUB pat(a$) CLS a$ = "NEPAL" CALL pat(a$) END SUB pat (s$) r = 1 t = 10 FOR i = 5 TO 1 STEP -2 PRINT TAB(t); MID$(s$, r, i) r = r + 2 t = t + 2 NEXT i END SUB
DECLARE FUNCTION pat$(a$) CLS a$ = "NEPAL" p$ = pat$(a$) END pat$ (s$) a$ = "NEPAL" r = 1 t = 10 FOR i = 5 TO 1 STEP -2 PRINT TAB(t); MID$(s$, r, i) r = r + 2 t = t + 2 NEXT i END FUNCTION
CLS s$ = "1A2b3c" FOR i = 1 TO LEN(s$) FOR j = 1 TO i PRINT MID$(s$, i, 1); NEXT j PRINT NEXT i END
DECLARE SUB pattern(s$) CLS s$ = "1A2b3c" CALL pattern(s$) END SUB pattern (s$) FOR i = 1 TO LEN(s$) FOR j = 1 TO i PRINT MID$(s$, i, 1); NEXT j PRINT NEXT i END SUB
DECLARE FUNCTION pattern$(s$) CLS r$ = pattern$(s$) END FUNCTION pattern$ (s$) s$ = "1A2b3c" FOR i = 1 TO LEN(s$) FOR j = 1 TO i PRINT MID$(s$, i, 1); NEXT j PRINT NEXT i END FUNCTION
import java.util.Scanner; class Rectangle4{ int l, b; void getData(){ Scanner in = new Scanner(System.in); System.out.print("Enter length : "); l=in.nextInt(); System.out.print("Enter breadth : "); b=in.nextInt(); } void displayArea(){ int a; a = l*b; System.out.println("Area = "+a); } public static void main(String args[]){ Rectangle4 obj = new Rectangle4(); obj.getData(); obj.displayArea(); } }
class Demo{ void display(){ System.out.println("Class Example"); } public static void main(String args[]){ Demo obj = new Demo(); obj.display(); } }
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset=UTF-8"> <title> A simple JSP program </title> </head> <body> <h1> Displaying "I love Programming 100 times !!" </h1> <table> <% for(int i=1; i<=100; i++){ %> <tr> <td> I Love Programming </td> </tr> <% } %> </table> </body> </html>
Output of the given Java program |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class PTR extends JFrame implements ActionListener //implement listener interface
{
JTextField t1, t2, t3, t4;
JLabel l1,l2,l3, l4;
JButton b1;
public PTR()
{
super("Handling Action Event");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("Enter P :");
l2 = new JLabel("Enter T :");
l3 = new JLabel("Enter R : ");
l4 = new JLabel("Interest : ");
t1 = new JTextField(10);
t2 = new JTextField(10);
t3 = new JTextField(10);
t4 = new JTextField(10);
b1 = new JButton("Calculate");
setLayout(new FlowLayout(FlowLayout.LEFT,150,10));
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(b1);
b1.addActionListener(this);//Registering event
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) //Handle Event
{
int p, t, r, si;
p = Integer.parseInt(t1.getText());
t = Integer.parseInt(t2.getText());
r = Integer.parseInt(t3.getText());
if(ae.getSource() == b1)
si = (p*t*r)/100;
else
si = 0;
t4.setText(String.valueOf(si));
}
public static void main(String a [])
{
new PTR();
}
}
import java.util.Scanner; class EvenArray{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int x, y; System.out.print("Enter First Number : "); x = in.nextInt(); System.out.print("Enter Second Number : "); y = in.nextInt(); for(int i=x; i<=y; i++){ if(i%2==0){ System.out.println(i); } } } }
The thread is a lightweight process that means one single program can be divided into small threads which will execute concurrently for fast execution of task. We can say that thread is a sub process of a process.
A thread is an independent path of execution within a program. Many thread can run concurrently within a program. Every threads in Java is created and controlled by the java.lang thread class. A java program can have many threads, and these threads can run currently, either synchronously or asynchronously.
- Threads are lightweight compared to process.
Difference between thread and process
Process | Thread |
---|---|
- A Process is an instance of a program. It contains a threads. | - Threads are the parts of process. It cannot contain a process. |
- Process run in its separate. | - Thread run in shared memory spaces. |
- Process is controlled by the operating system. | - Threads a re controlled by programmer in a program. |
- Processes are independent. | - Threads are dependent. |
- New processes require duplication of the parent process. | - New threads are easily created. |
- Process require more time for context switching as they are more heavy. | - Threads require less time for context switching as they are lighter then process. |
- Process require more resources then threads. | - Threads generally need less resources than process. |
- Process require more time for termination. | - Threads require less time for termination. |
Life cycle of threads
New
The thread is in new state if you create an instance of thread class but before the invocation of start() method.
Runnable
A thread in the runnable state is executing the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
Running
The thread is in running state if the thread scheduler has selected it.
Blocked (Non Runnable)
It is the state when the thread is still alive, but is currently not eligible to run.
Terminated
A thread is in terminated or dead state when its run() method exists.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Student extends JFrame implements ActionListener
{
JLabel sid,sname,slevel;
JTextField tid,tname, tlevel;
JButton insert, clear;
JPanel p1,p2,p3,p4;
Student()
{
setSize(400,250);
setTitle("Students Data Entry");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
setLayout(new BorderLayout());
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
p1.setLayout(new GridLayout(1,2));
p1.add(p3);
p1.add(p4);
p3.setLayout(new FlowLayout(FlowLayout.LEFT, 75,20));
p4.setLayout(new FlowLayout(FlowLayout.LEFT, 25,20));
sid=new JLabel("Student ID");
sname=new JLabel("Student Name");
slevel=new JLabel("Level");
p3.add(sid);
p3.add(sname);
p3.add(slevel);
tid=new JTextField(10);
tname=new JTextField(10);
tlevel=new JTextField(10);
p4.add(tid);
p4.add(tname);
p4.add(tlevel);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 20,20));
insert=new JButton("Insert");
clear=new JButton("Clear");
p2.add(insert);
p2.add(clear);
insert.addActionListener(this);
clear.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Connection c=null;
Statement s=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://localhost/mydb","root", "raj");
s=c.createStatement();
if(ae.getSource()==insert)
{
int id;
String name, level;
id=Integer.parseInt(tid.getText());
name=tname.getText();
level=tlevel.getText();
String query="insert into studentdb values("+id+",'"+name+"',"+level+")";
s.executeUpdate(query);
JOptionPane.showMessageDialog(this,"Data is Recorded!!!!");
}
if(ae.getSource()==clear)
{
tid.setText("");
tname.setText("");
tlevel.setText("");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String a[])
{
new Student();
}
}
SISD Uni-Processor Architecture |
SIMD Architecture (With Distributed Memory) |
MISD Architecture (The systolic Array) |
MIMD Architecture (With shared Memory) |
virtual memory is a section of a hard disk that's set up to emulate the computer's RAM. |
#define N 100 /* number of slots in the buffer */ typedef int semaphore; /* semaphores are a special kind of int */ semaphore mutex = 1; /* controls access to critical region */ semaphore empty = N; /* counts empty buffer slots */ semaphore null = 0; /* counts full buffer slots */ void producer(void){ int item; message m; // message buffer. while(TRUE){ item = produce_item(); // generate something to put in buffer receive(consumer, &m); // Receive an empty message, if any build_message(&m, item); // construct a message to send send(consumer, &m); // send item to consumer } } void consumer(void){ int item, i; message m; for(i=0; i<N; i++) send(producer, &m); // send N empty messages while(TRUE){ receive(produce, &m); // get message containing item item = extract_item(&m); // extract item from message send(producer, &m); //send back empty replay consumer_item(term); //do something with the item } }
The shell is an interface between user and the operating system |
Thread | Process |
---|---|
- Threads are lightweight | - Processes are heavyweight |
- Threads are runs in address space of processes | - Processes have their own address space |
- Threads shares files and other data | - Processes do not share files and other data |
- Thread switching is faster | - Process switching is slower |
Threads are easy to create and destroy | Process are difficult to create and destroy |