Thread runner in java

multi-thread run processes simultaneously

I am currently interested in learning about Multi-threading. I have written a piece of code. And my question is if this the correct way. I later want to implement this in another code of mine. My idea was to run multiple threads existing of the same class. So let’s say 5 threads and they all have a runner class inside them. What I want to implement in another code, is that I want to run multiple browsers at once. So, I have a class Browser. And then I have 5 threads with the class Browser. And they are all working simultaneously but don’t know each other, so they don’t have any interaction with each other at all. So is this idea OK then?

class Runner implements Runnable < public void run() < for(int i = 0; i < 10; i++) < System.out.println(Thread.currentThread().getName() + " - " + i); try < Thread.sleep(100); >catch (InterruptedException e) < e.printStackTrace(); >> > > public class BasisDemo < public static void main(String[] args) < Thread runner1 = new Thread(new Runner(), "Runner 1"); runner1.start(); Thread runner2 = new Thread(new Runner(), "Runner 2"); runner2.start(); >> 

Is this a good practise and does it support my idea? Or is using multi-threading even unnecessary. I don’t know it for sure. Thanks for helping me out.

@JunbangHuang yes that’s correct. But they do have to start all at once. And they have to do their jobs 24/7 they are all from the same objects let’s say Browser as example. So 5 browsers start at once. And they have to do tasks for an hour and then sleep for a day as example. Because the app is running 24/7 in the background. Thanks for your comment.

Читайте также:  Php файл удаленный сервер

so if I have 5 browsers, one have 5 threads inside each, and you want 25 threads start at the same time?

Oh no the idea is very simple. Just refer to my code example of the runner in the description. Just 5 threads like, example: Thread browser1 = new Thread(new Browser(), «Browser 1»); . They have to stay active 24/7 and do tasks in a certain hour.

actually keeping alive threads don’t sound good.But it depends on the purpose of the bussiness.When and In which condition your thread comes back to continue process?Sometimes you need to schedule timertask so when the time is ready thread is being created and computing but sometimes you need to keep alive it like in Network listening.So please share your bussiness logic.

Источник

Pass string between two threads in java

I have to search a string in a file and write the matched lines to another file. I have a thread to read a file and a thread to write a file. I want to send the stringBuffer from read thread to write thread. Please help me to pass this. I amm getting null value passed. write thread:

class OutputThread extends Thread < /****************** Writes the line with search string to the output file *************/ Thread runner1,runner; File Out_File; public OutputThread() < >public OutputThread(Thread runner,File Out_File) < runner1 = new Thread(this,"writeThread"); // (1) Create a new thread. this.Out_File=Out_File; this.runner=runner; runner1.start(); // (2) Start the thread. >public void run() < try< BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(Out_File,true)); System.out.println("inside write"); synchronized(runner)< System.out.println("inside wait"); runner.wait(); >System.out.println("outside wait"); // bufferedWriter.write(line.toString()); Buffer Buf = new Buffer(); bufferedWriter.write(Buf.buffers); System.out.println(Buf.buffers); bufferedWriter.flush(); > catch(Exception e) < System.out.println(e); e.printStackTrace(); >> > 
class FileThread extends Thread < Thread runner; File dir; String search_string,stats; File Out_File,final_output; StringBuffer sb = new StringBuffer(); public FileThread() < >public FileThread(CountDownLatch latch,String threadName,File dir,String search_string,File Out_File,File final_output,String stats) < runner = new Thread(this, threadName); // (1) Create a new thread. this.dir=dir; this.search_string=search_string; this.Out_File=Out_File; this.stats=stats; this.final_output=final_output; this.latch=latch; runner.start(); // (2) Start the thread. >public void run() < try< Enumeration entries; ZipFile zipFile; String source_file_name = dir.toString(); File Source_file = dir; String extension; OutputThread out = new OutputThread(runner,Out_File); int dotPos = source_file_name.lastIndexOf("."); extension = source_file_name.substring(dotPos+1); if(extension.equals("zip")) < zipFile = new ZipFile(source_file_name); entries = zipFile.entries(); while(entries.hasMoreElements()) < ZipEntry entry = (ZipEntry)entries.nextElement(); if(entry.isDirectory()) < (new File(entry.getName())).mkdir(); continue; >searchString(runner,entry.getName(),new BufferedInputStream(zipFile.getInputStream(entry)),Out_File,final_output,search_string,stats); > zipFile.close(); > else < searchString(runner,Source_file.toString(),new BufferedInputStream(new FileInputStream(Source_file)),Out_File,final_output,search_string,stats); >> catch(Exception e) < System.out.println(e); e.printStackTrace(); >> /********* Reads the Input Files and Searches for the String ******************************/ public void searchString(Thread runner,String Source_File,BufferedInputStream in,File output_file,File final_output,String search,String stats) < int count = 0; int countw = 0; int countl=0; String s; String[] str; String newLine = System.getProperty("line.separator"); try < BufferedReader br2 = new BufferedReader(new InputStreamReader(in)); //OutputFile outfile = new OutputFile(); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(output_file,true)); Buffer Buf = new Buffer(); //StringBuffer sb = new StringBuffer(); StringBuffer sb1 = new StringBuffer(); while((s = br2.readLine()) != null ) < str = s.split(search); count = str.length-1; countw += count; if(s.contains(search))< countl++; sb.append(s); sb.append(newLine); >if(countl%100==0) < System.out.println("inside count"); Buf.setBuffers(sb.toString()); sb.delete(0,sb.length()); System.out.println("outside notify"); synchronized(runner) < runner.notify(); >//outfile.WriteFile(sb,bufferedWriter); //sb.delete(0,sb.length()); > > > synchronized(runner) < runner.notify(); >br2.close(); in.close(); if(countw == 0) < System.out.println("Input File : "+Source_File ); System.out.println("Word not found"); System.exit(0); >else < System.out.println("Input File : "+Source_File ); System.out.println("Matched word count : "+countw ); System.out.println("Lines with Search String : "+countl); System.out.println("Output File : "+output_file.toString()); System.out.println(); >> catch(Exception e) < System.out.println(e); e.printStackTrace(); >> > 

Источник

Оцените статью