Results 1 to 9 of 9
Thread: multithreading
- 03-10-2011, 07:14 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
multithreading
Hi Frnds.... I am attaching one file where we use the concept of multithreading...In this suppose I have a text which is aabbccddeeffgghh and when I am searching for the pattern "aa" or "cc" its displaying the position where it is found but when I am searching for "bb" or "dd" or "hh" its nt displaying its position and all.......could any help me out....
Java Code:import java.util.Scanner; import java.util.Arrays; import java.lang.String; import java.io.*; public class rabinkarp2 { public static void main(String args[]) { String text1; String pattern1; long time; Scanner s=new Scanner(System.in); System.out.println("\n Enter the text"); text1=s.nextLine(); System.out.println("\n Enter the pattern"); pattern1=s.nextLine(); time = System.currentTimeMillis(); karprabin b1=new karprabin(text1,pattern1); NewThread1 n1=new NewThread1(b1,0,text1.length()/4); NewThread1 n2=new NewThread1(b1,text1.length()/4-pattern1.length()+1,text1.length()/4+pattern1.length()-1); NewThread1 n3=new NewThread1(b1, text1.length()/4,text1.length()/2); NewThread1 n4=new NewThread1(b1,text1.length()/2-pattern1.length()+1,text1.length()/2+pattern1.length()-1); NewThread1 n5=new NewThread1(b1, text1.length()/2, (int)((0.75)*(text1.length()))); NewThread1 n6=new NewThread1(b1,(int)((0.75)*(text1.length()))-pattern1.length()+1,(int)((0.75)*(text1.length()))+pattern1.length()-1); NewThread1 n7=new NewThread1(b1,(int)((0.75)*(text1.length())),text1.length()); try { n1.t1.join(); n2.t1.join(); n3.t1.join(); n4.t1.join(); n5.t1.join(); n6.t1.join(); n7.t1.join(); } catch(Exception ex) {} time = System.currentTimeMillis()-time; System.out.println(" \nThe test took " + time + " milliseconds"); NewThread1.display(); }// end main }
Java Code:class karprabin { String text,pattern; karprabin(){} public static int arrayCmp(char pat[], int aIdx,char txt[], int bIdx,int len) { int i = 0; for (i = 0; i < len && (aIdx + i) < pat.length && (bIdx + i) < txt.length; i++) { if (pat[aIdx + i] == txt[bIdx + i]) ; else if (pat[aIdx + i] > txt[bIdx + i]) return 1; else return 2; } if (i < len) { if (pat.length - aIdx == txt.length - bIdx) return 0; else if (pat.length - aIdx > txt.length - bIdx) return 1; else return 2; } else return 0; } public static int rehash(char a,char b,int h,int d) { return ((((h)-(a)*d)<<1)+(b)); } karprabin(String text,String pattern) { this.text=text; this.pattern=pattern; int n=text.length(); int m=pattern.length(); if(m>n/4) { System.out.println("pattern length is greater than text size"); System.exit(0); } }//end karp rabin }//end class
Java Code:class NewThread1 extends karprabin implements Runnable { String name,text,pattern; Thread t1; karprabin b11; int low, high,hx,hy; static int count=0; NewThread1(karprabin b, int low, int high) { b11=b; text=b11.text; pattern=b11.pattern; this.low=low; this.high=high; t1=new Thread(this); t1.start(); } NewThread1() { } public void run() { try { int d,i,j; int m=pattern.length(); int n=text.length(); String sub=text.substring(low,high); char y[]=sub.toCharArray(); char x[]=pattern.toCharArray(); //preprocessing--computing d=2^(m-1) with the left shift operator for(d=i=1;i<m;++i) d=(d<<1); for(hy=hx=i=0;i<m;++i) { hx=((hx<<1)+x[i]); hy=((hy<<1)+y[i]); } j=0; for(i=low;i<high;i++) { while(hx==hy && karprabin.arrayCmp(x,0,y,j,m)==0 && (i+j)<high) { NewThread1.count++; System.out.printf("\n match found between at %d\n",low+j+1); } hy=karprabin.rehash(y[j],y[j+m],hy,d); j++; } } catch(ArrayIndexOutOfBoundsException e) { } catch(StringIndexOutOfBoundsException e) { } } public static void display() { System.out.printf("\n total matches found = %d",NewThread1.count); } }
Last edited by Eranga; 03-12-2011 at 01:55 AM. Reason: code tags added
- 03-11-2011, 08:15 PM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
Hi Praveen,
From ur post its not very clear as to what exactly you want to do.
If just want to find the location(index) of a pattern in a file or string, the why do you need so many threads?
I am a bit confused :-(
Could you please elaborate
- 03-12-2011, 01:54 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Can you elaborate that what you are doing with this?
Java Code:NewThread1 n1=new NewThread1(b1,0,text1.length()/4); NewThread1 n2=new NewThread1(b1,text1.length()/4-pattern1.length()+1,text1.length()/4+pattern1.length()-1); NewThread1 n3=new NewThread1(b1, text1.length()/4,text1.length()/2); NewThread1 n4=new NewThread1(b1,text1.length()/2-pattern1.length()+1,text1.length()/2+pattern1.length()-1); NewThread1 n5=new NewThread1(b1, text1.length()/2, (int)((0.75)*(text1.length()))); NewThread1 n6=new NewThread1(b1,(int)((0.75)*(text1.length()))-pattern1.length()+1,(int)((0.75)*(text1.length())) +pattern1.length()-1); NewThread1 n7=new NewThread1(b1,(int)((0.75)*(text1.length())),text1 .length());
- 03-14-2011, 03:03 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
multithreading
actually i am splitting the text into various halves and running each half as a single thread......i got the answer to above question which i have asked..........i solved the above problem by using substring method.......
now my next question is how to read all the lines of a file using bufferedReader....and to assign that value to string text1 which is the variable i have used in my program............
- 03-14-2011, 07:11 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Before that the idea of a thread is to take care of a single process, without effecting to the others. Resources may or may not used, depend on the design.
So in your case you can maintain one thread to read the file, and submit your query to process through different threads.
- 03-14-2011, 07:14 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Java Code://Construct the BufferedReader object, filename is string Object bufferedReader = new BufferedReader(new FileReader(filename)); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); // Just print the line }
- 04-12-2011, 10:58 AM #7
where do you start this threads? usually when the a class implements Runnable you must start this class with
Thread t1 = new Thread(new ClassWithRunnable());
t1.start();
you can solve your problem without using threads but regular expressions, read this tutorialLast edited by j2me64; 04-12-2011 at 11:00 AM.
- 04-17-2011, 04:00 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
I am creating a server with two ports , one port is acting as a client of type teacher , while the other is of type student. I need the teacher ( class conacceptor one) to choose a certain student (class conacceptor two) .
and that's my code :
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class omar {
private ServerSocket socketA;
private Runnable accepterA;
private ServerSocket socketB;
private Runnable accepterB;
ObjectOutputStream out;
ObjectInputStream in;
String message;
String fileName;
String resala;
ObjectOutputStream out2;
ObjectInputStream in2;
String message2;
String fileName2;
String resala2;
public omar(int portA, int portB) throws IOException
{
if (portA == portB)
throw new IllegalArgumentException("Ports can't be equal");
System.out.println("waiting for connection");
socketA = new ServerSocket(portA);
socketB = new ServerSocket(portB);
accepterA = new ConnAccepter(socketA);
accepterB = new ConnAccepter2(socketB);
Thread tA = new Thread(accepterA);
Thread tB = new Thread(accepterB);
tA.start();
tB.start();
}
class ConnAccepter implements Runnable {
private ServerSocket sock;
ConnAccepter(ServerSocket s) {
sock = s;
}
public void run() {
Socket newsock;
while(true) {
newsock = null;
//connection //
try {
newsock = sock.accept();
//st x= new st(socket);
//start();
//start new thread ...server_teacherConn(socket)
System.err.println("Connection from " + newsock +" for " + Thread.currentThread().getName());
}catch (IOException e) {
System.err.println("Bad accept in " + Thread.currentThread().getName());
}
// end connection , attach output and input stream //
try{
in = new ObjectInputStream(newsock.getInputStream());
out = new ObjectOutputStream(newsock.getOutputStream());
}
catch(Throwable c)
{
System.out.println("error1");
}
}
}
}
class ConnAccepter2 implements Runnable {
private ServerSocket sock;
Hashtable ip= new Hashtable();
ConnAccepter2(ServerSocket s) {
sock = s;
}
public void run() {
Socket newsock;
while(true) {
newsock = null;
try {
newsock = sock.accept();
System.err.println("Connection from " + newsock +" for " + Thread.currentThread().getName());
}catch (IOException e) {
System.err.println("Bad accept in " + Thread.currentThread().getName());
}
try{
out2 = new ObjectOutputStream(newsock.getOutputStream());
in2 = new ObjectInputStream(newsock.getInputStream());
}
catch(Throwable c)
{
System.out.println("error1");
}
try{
String lolo=(String)in2.readObject();
}
catch(Throwable c)
{
System.out.println("error1");
System.exit(0);
}
try{
String momo=(String)in.readObject();
Socket connectionSocket31=(Socket)ip.get(momo);
Thread tC = new Thread(new StudentThread(connectionSocket31));
tC.start();
}
catch(Throwable c)
{
System.out.println("error2");
System.exit(0);
}
}
}
}
class StudentThread implements Runnable{
private Socket sock;
StudentThread(Socket s) {
sock=s;
}
public void run(){
// //
while(true){
try{
String momo=(String)in.readObject();// ERROR
out2.writeObject((momo));
out2.flush();
if(momo.equals("application1")){
out2.writeObject((String)in.readObject());
out2.writeObject((String)in.readObject());
out2.writeObject((String)in.readObject());
out2.writeObject((String)in.readObject());
out2.flush();
out.writeObject((String)in2.readObject());
}
else{
out.writeObject((String)in2.readObject());
}
}
catch(Throwable c)
{
System.out.println("error2");
System.exit(0);
}
}
}
}
public static void main(String [] args) {
omar server;
try {
server = new omar(6000, 9000);
} catch(IOException ie) {
System.err.println("Could not start server: " + ie);
}
}
}
-
Similar Threads
-
Multithreading Gui
By BUGSIE91 in forum Threads and SynchronizationReplies: 7Last Post: 10-13-2010, 03:20 PM -
Want to know about Multithreading.
By Chetans in forum Threads and SynchronizationReplies: 1Last Post: 03-19-2010, 08:50 AM -
Log 4j Multithreading
By joe2010 in forum Threads and SynchronizationReplies: 1Last Post: 01-31-2010, 04:48 AM -
multithreading
By shilpa.krishna in forum New To JavaReplies: 2Last Post: 06-27-2008, 05:18 PM
Bookmarks