Login or Register

  Welcome: Guest


Home >> Article >> Programming >> Java >> Post New Article Bookmark and Share

Threads in Java

June 23, 2010  by: Karthik  Points: 10   Category: Java  Earning $0.10   Views: 689

Here is the most commonly used concept i.e. threads in Java.

         

Hi,

One of the most commonly used concept in Java is threads and below is the program for description of threads in Java by splitting up the entire text into five smaller text and finally adding all the words.

Program for threads in Java

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));
}
}

Regards,
Karthik.

Tags:



Comment(s)

No response found. Be the first to respond to this post

Post Comment

Please Log In  to post comment

Related Resources



About Java
Java is a purpose object oriented programming language

Enterprise Java Beans : An Overview
This article contains basic information about Enterprise Java Beans (EJBs)Technology .

Application Deployment Tool in java
Java Based Tools for testing and deployment tool

Tips for learning java to face an interview related to C and C++
Tips,learning java,java interview,with c

Usage of Java Programming and Concepts
Describes the salient features of Java and explains about the usage of java.

Latest Resources From The Same Category



Use of switch in java programming
Switch is a conditional statement which used to pick a statement if the condition and expression are equal. This article explains about the use and functioning switch along with example.

Addition of 2 numbers in java program
For starting with Java we need to know 2 things first is how to print a message and how to accept 2 numbers & print there sum. This article is about how to create a program which accept 2 numbers and print there sum.

Starting Java Programming
Java is computer language. It help the user to develop softwares,programs and even games also. It is easy to study this computer language. Sometimes you may see certain title says java games, java apps when you try to download mobile apps from mobile store. That means this app is made with java language. This article is about how to write and run a java program.

Usage of Java Programming and Concepts
Describes the salient features of Java and explains about the usage of java.

Threads in Java
Here is the most commonly used concept i.e. threads in Java.

 

Top Performers     Top Contributors     Share and Earn     Terms of Service    Privacy Policy     Contact Us