skip to Main Content

I tried to use AsyncTask but is deprecated and i dont understand how to use Executors can someone help me how can use it with just small example
my code below

    public void onClick(View view) {
         String obj = this.txtone.getText().toString();
         String obj2 = this.txttwo.getText().toString();
         String obj3 = this.txttime.getText().toString();


        Double value2 = Double.valueOf(obj); 
        Double value3 = Double.valueOf(obj2);
        Double value4 = Double.valueOf(obj3); 

        Double valueOf = value4;

        this.table.removeAllViews();
        NumberFormat instance = NumberFormat.getInstance(Locale.US);
        String[] strArr = {"Times " , "number 1","number 1" ,"Sum"};
        addTableRowHeader(strArr[0], strArr[1], strArr[2], strArr[3]);
        String str = "";
        int i = 0;
        while (((double) i) < valueOf.doubleValue()) {
            double sum = value2 + value3;
            StringBuilder stringBuilder;
            stringBuilder = new StringBuilder(str);
            i++;
            stringBuilder.append(i);
            addTableRow(stringBuilder.toString(), instance.format(value2), instance.format(value3), instance.format(sum));
            valueOf2 = sum;
}

2

Answers


  1. You can use Executors:

    For Multi Threading :

    private static final int CPU_COUNT = 
    Runtime.getRuntime().availableProcessors();
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private final ExecutorService service = 
    Executors.newFixedThreadPool(MAXIMUM_POOL_SIZE);
    

    For Single Thread :

    private final ExecutorService  service = 
    Executors.newSingleThreadExecutor();
    

    Now simply execute your work using execute() function like this :

     service.execute(new Runnable{
    
     // Fetch user data here or do whatever you want to do on 
      background thread.
    // This is same as doInBackground method of AsyncTask which 
     executes on the 
       background thread.
    
    runOnUiThread(new Runnable{
    
    //This Section will Execute on Main thread.
    // Use this if you want to access any UI element.
    });
    
    
    });
    

    Just remember to shutdown the Executor when you are finished like this :

    service.shutdown(); 
    
    Login or Signup to reply.
  2. This can also be used

    public void onClick(View view) {
    String obj = this.txtone.getText().toString();
    String obj2 = this.txttwo.getText().toString();
    String obj3 = this.txttime.getText().toString();
    
    final Double value2 = Double.valueOf(obj);
    final Double value3 = Double.valueOf(obj2);
    final Double value4 = Double.valueOf(obj3);
    
    final Double valueOf = value4;
    
    this.table.removeAllViews();
    final NumberFormat instance = NumberFormat.getInstance(Locale.US);
    final String[] strArr = {"Times ", "number 1", "number 1", "Sum"};
    addTableRowHeader(strArr[0], strArr[1], strArr[2], strArr[3]);
    
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    
    for (int i = 0; i < valueOf; i++) {
        final int index = i;
        executorService.submit(() -> {
            double sum = value2 + value3;
            runOnUiThread(() -> {
                addTableRow(String.valueOf(index + 1), instance.format(value2), instance.format(value3), instance.format(sum));
            });
        });
    }
    
    
      executorService.shutdown();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search