Skip to content

Instantly share code, notes, and snippets.

@yuikns
Created August 14, 2014 07:04
Show Gist options
  • Save yuikns/53493e91a670568c600f to your computer and use it in GitHub Desktop.
Save yuikns/53493e91a670568c600f to your computer and use it in GitHub Desktop.
package com.argcv.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MultiThreadDemo {
int counter = 0;
public class InsertThread extends Thread {
public List<String> dataToInsert;
public InsertThread(List<String> dataToInsert) {
this.dataToInsert = dataToInsert;
}
public void run() {
counter++;
for (String s : dataToInsert) {
System.out.println("insert :" + s);
}
counter--;
}
}
public class DataReader extends Thread {
String fileName;
public DataReader(String fileName) {
this.fileName = fileName;
}
public void run() {
List<String> dataToInsert = new ArrayList<String>();
String s = sc.next();
System.out.println("add :" + s);
dataToInsert.add(s);
if (dataToInsert.size() > 10) {
InsertThread is = new InsertThread(dataToInsert);
is.start();
isList.add(is);
dataToInsert = new ArrayList<String>();
}
}
}
public MultiThreadDemo() {
Scanner sc = new Scanner(System.in);
List<DataReader> isList = new ArrayList<DataReader>();
for (int x = 0; x < 160; x++) {
String s = sc.next();
System.out.println("add :" + s);
DataReader dr = new DataReader(s);
dr.start();
isList.add(dr);
while (isList.size() > 5) {
for (int i = 0; i < isList.size(); i++) {
if (!isList.get(i).isAlive()) {
isList.remove(i);
i--;
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
new MultiThreadDemo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment