Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, December 15, 2010

Java Threads

Introduction to Threads

Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java 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.
Multithreading has several advantages over Multiprocessing such as;
  • Threads are lightweight compared to processes
  • Threads share the same address space and therefore can share both data and code
  • Context switching between threads is usually less expensive than between processes
  • Cost of thread intercommunication is relatively low that that of process intercommunication
  • Threads allow different tasks to be performed concurrently.
The following figure shows the methods that are members of the Object and Thread Class.

Thread Creation

There are two ways to create thread in java;
  • Implement the Runnable interface (java.lang.Runnable)
  • By Extending the Thread class (java.lang.Thread)

Implementing the Runnable Interface

The Runnable Interface Signature
public interface Runnable {
void run();
}
One way to create a thread in java is to implement the Runnable Interface and then instantiate an object of the class. We need to override the run() method into our class which is the only method that needs to be implemented. The run() method contains the logic of the thread.
The procedure for creating threads based on the Runnable interface is as follows:
1. A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object.
2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.
3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned.
4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.
Below is a program that illustrates instantiation and running of threads using the runnable interface instead of extending the Thread class. To start the thread you need to invoke the start() method on your object.
class RunnableThread implements Runnable {

 Thread runner;
 public RunnableThread() {
 }
 public RunnableThread(String threadName) {
  runner = new Thread(this, threadName); // (1) Create a new thread.
  System.out.println(runner.getName());
  runner.start(); // (2) Start the thread.
 }
 public void run() {
  //Display info about this particular thread
  System.out.println(Thread.currentThread());
 }
}

public class RunnableExample {

 public static void main(String[] args) {
  Thread thread1 = new Thread(new RunnableThread(), "thread1");
  Thread thread2 = new Thread(new RunnableThread(), "thread2");
  RunnableThread thread3 = new RunnableThread("thread3");
  //Start the threads
  thread1.start();
  thread2.start();
  try {
   //delay for one second
   Thread.currentThread().sleep(1000);
  } catch (InterruptedException e) {
  }
  //Display info about the main thread
  System.out.println(Thread.currentThread());
 }
}
Output
thread3
Thread[thread1,5,main]
Thread[thread2,5,main]
Thread[thread3,5,main]
Thread[main,5,main]private
Download Runnable Thread Program Example
This approach of creating a thread by implementing the Runnable Interface must be used whenever the class being used to instantiate the thread object is required to extend some other class.

Extending Thread Class

The procedure for creating threads based on extending the Thread is as follows:
1. A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.
2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.
3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.
Below is a program that illustrates instantiation and running of threads by extending the Thread class instead of implementing the Runnable interface. To start the thread you need to invoke the start() method on your object.
class XThread extends Thread {

 XThread() {
 }
 XThread(String threadName) {
  super(threadName); // Initialize thread.
  System.out.println(this);
  start();
 }
 public void run() {
  //Display info about this particular thread
  System.out.println(Thread.currentThread().getName());
 }
}

public class ThreadExample {

 public static void main(String[] args) {
  Thread thread1 = new Thread(new XThread(), "thread1");
  Thread thread2 = new Thread(new XThread(), "thread2");
  //     The below 2 threads are assigned default names
  Thread thread3 = new XThread();
  Thread thread4 = new XThread();
  Thread thread5 = new XThread("thread5");
  //Start the threads
  thread1.start();
  thread2.start();
  thread3.start();
  thread4.start();
  try {
 //The sleep() method is invoked on the main thread to cause a one second delay.
   Thread.currentThread().sleep(1000);
  } catch (InterruptedException e) {
  }
  //Display info about the main thread
  System.out.println(Thread.currentThread());
 }
}
Output
Thread[thread5,5,main]
thread1
thread5
thread2
Thread-3
Thread-2
Thread[main,5,main]
Download Java Thread Program Example
When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:
  • Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface
    has this option.
  • A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive.
An example of an anonymous class below shows how to create a thread and start it:
( new Thread() {
public void run() {
for(;;) System.out.println(”Stop the world!”);
}
}
).start();

Thursday, November 11, 2010

JAVA Technical Consultant

JAVA Technical Consultant (JAVA1)


Responsibilities

· An Oracle Java Technical Consultant is responsible for

· Developing using ADF, J2EE, JTA, Core Java

· Developing and Customized OAF (Oracle Application Framework) Pages within Oracle Application

Qualifications & Skills

· Have a strong knowledge in SQL & PLSQL Language

· Have a good Knowledge in J-Developer

· Have a good Knowledge for Oracle E-Business Suite.

· Developing and Customized Using OAF


· Oracle SOA Suite is an advantage

· Oracle developer tools (Report Builder & Form Builder) is an advantage


· At least one year experience
To apply for the above vacancies, please e-mail your CV with photo as a word attachment or PDF, stating the title of the vacancy in the subject line.

SphereConsulting@yahoo.com



Java Developers @ iCraft

Requirement

1- Bachelor's degree in Software Engineering or closely related degree.
2- Good working Experience with web development using the Java EE technology.
3- Java 5.0 experience and knowledge.
4- Enterprise Java Bean 3.0.
5- Good Knowledge of design patterns.
6- Experience with Hibernate and ORM concepts in general.
7- Experience with Build systems like Maven and/or ANT.
8- JSF and Rich Faces knowledge and experience.
9- Experience with Subversion.
10- Good knowledge of XML and Webservices.
11- Knowledge and experience with the following products is an advantage Apache Tomcat, JBoss.
How to Apply
please apply online by sending
fill online form on http://spreadsheets4. google.com/viewform?formkey= dHVQdUZwWThLcDkwVkoxQ2lyWjF0VH c6MA

send your CV to hr@icraft-eg.com

Tuesday, November 9, 2010

JAVA Technical Consultant (JAVA1)

JAVA Technical Consultant (JAVA1)

Responsibilities

• An Oracle Java Technical Consultant is responsible for

• Developing using ADF, J2EE, JTA, Core Java

• Developing and Customized OAF (Oracle Application Framework) Pages within Oracle Application

Qualifications & Skills

• Have a strong knowledge in SQL & PLSQL Language

• Have a good Knowledge in J-Developer

• Have a good Knowledge for Oracle E-Business Suite.

• Developing and Customized Using OAF

• Oracle SOA Suite is an advantage

• Oracle developer tools (Report Builder & Form Builder) is an advantage

• At least one year experience

please e-mail your CV with photo as a word attachment or PDF, stating the title of the vacancy in the subject line.



Tuesday, October 26, 2010

Send Email in Java

The JavaMail API is not part of core Java SE, but an optional extension. In addition, it is required in Java Enterprise Edition. The JavaMail packages can be accessed in two ways :
by placing j2ee.jar in the classpath
or, by placing both mail.jar and activation.jar in the classpathThe javax.mail API uses a properties file for reading server names and related configuration. These settings will override any system defaults. Alternatively, the configuration can be set directly in code, using the JavaMail API.
Example
An email configuration file (to run the example, substitute valid values for the uncommented items) :
# Configuration file for javax.mail # If a value for an item is not provided, then # system defaults will be used. These items can # also be set in code.
# Host whose mail services will be used # (Default value : localhost) mail.host=mail.blah.com
# Return address to appear on emails # (Default value : username@host) mail.from=webmaster@blah.net
# Other possible items include: # mail.user= # mail.store.protocol= # mail.transport.protocol= # mail.smtp.host= # mail.smtp.user= # mail.debug=
A class which uses this file to send an email : import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
* Simple demonstration of using the javax.mail API.
*
* Run from the command line. Please edit the implementation
* to use correct email addresses and host name.
*/
public final class Emailer {
public static void main( String... aArguments ){
Emailer emailer = new Emailer();
//the domains of these email addresses should be valid,
//or the example will fail:
emailer.sendEmail(
"fromblah@blah.com", "toblah@blah.com",
"Testing 1-2-3", "blah blah blah"
);
}
/**
* Send a single email.
*/
public void sendEmail(
String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody
){
//Here, no Authenticator argument is used (it is null).
//Authenticators are used to prompt the user for user
//name and password.
Session session = Session.getDefaultInstance( fMailServerConfig, null );
MimeMessage message = new MimeMessage( session );
try {
//the "from" address may be set in code, or set in the
//config file under "mail.from" ; here, the latter style is used
//message.setFrom( new InternetAddress(aFromEmailAddr) );
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject( aSubject );
message.setText( aBody );
Transport.send( message );
}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}
/**
* Allows the config to be refreshed at runtime, instead of
* requiring a restart.
*/
public static void refreshConfig() {
fMailServerConfig.clear();
fetchConfig();
}
// PRIVATE //
private static Properties fMailServerConfig = new Properties();
static {
fetchConfig();
}
/**
* Open a specific text file containing mail server
* parameters, and populate a corresponding Properties object.
*/
private static void fetchConfig() {
InputStream input = null;
try {
//If possible, one should try to avoid hard-coding a path in this
//manner; in a web application, one should place such a file in
//WEB-INF, and access it using ServletContext.getResourceAsStream.
//Another alternative is Class.getResourceAsStream.
//This file contains the javax.mail config properties mentioned above.
input = new FileInputStream( "C:\\Temp\\MyMailServer.txt" );
fMailServerConfig.load( input );
}
catch ( IOException ex ){
System.err.println("Cannot open and load mail server properties file.");
}
finally {
try {
if ( input != null ) input.close();
}
catch ( IOException ex ){
System.err.println( "Cannot close mail server properties file." );
}
}
}
}

Thursday, March 12, 2009

اسطوانات تعليمية مجانا من AppDev

للحصول على اسطوانات تدريبية مجانا مقدمة من شركة AppDev
يرجي الضغط على هذا الرابط
http://www.appdev.com/promo/freetitle.asp?PC=RN00848&T=d%5FAJAX
والتسجيل وستجد مجموعة كبيرة من الاسطوانات التي يمكن تحميلها