mardi 28 janvier 2020

Can i adding new child class at run time?

I need to add the following class at run time:

public class MapperFunction extends Mapper<Integer, String, String, Integer> {


    public MapperFunction(InputBlock s) {
        super(s);
    }

    @Override
    protected void map(Integer key, String value) {
        Pattern pattern = Pattern.compile("[a-zA-Z]+");
        Matcher matcher;
        String str = value;
        if (!str.equals("")) {
            matcher = pattern.matcher(str);
            while (matcher.find()) {
                String word = matcher.group();
                if (!MapperOut.containsKey(word))
                    MapperOut.put(word, 1);
                else
                    MapperOut.put(word, (Integer) MapperOut.get(word) + 1);
            }
        }
    }
}

or add the method map() to class during run time. i read the following question on stackoverflow Extending or adding new classes at runtime in Java but i think my case different a bit, any way this is the parent class Mapper:

public abstract class Mapper<keyIn, valueIn, keyOut, valueOut> extends Thread {

    private RecordReader recordReader;
    static AtomicInteger numberOfThreadsPerMapper = new AtomicInteger(2);
    static Map<Object, Object> MapperOut = null;

    static {
        MapperOut = Collections.synchronizedMap(new TreeMap<>());
    }

    Mapper(InputBlock s) {
        recordReader = new LineRecordReader(s);
    }

    protected abstract void map(keyIn key, valueIn value) throws IOException, InterruptedException;

    @Override
    public void run() {
        run2();
    }

    public void run2() {
        try {
            while (recordReader.hasNext()) {
                map((keyIn) recordReader.getKey(), (valueIn) recordReader.getValue());
            }
            //   System.out.println("Thread out." + numberOfThreads.getAndIncrement());
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }
}

note that the parent class Mapper extends Thread my second question if i can do that how i can create instance from it, now this my call to create:

  @Override
    public void update(Observable o, Object arg) {

        executorService.submit(new MapperFunction((InputBlock) arg));
    }

my last questions if all that can be happen is there any disadvantages ?(performance issue)since the application create more instance from Mapper class?





Aucun commentaire:

Enregistrer un commentaire