Progress bar in multi-thread implementation

Hi

I’m trying to implement a ProgressBar in a multithreaded routine, however it and Notification are only sensitized after running all threads. How do I get these components to run correctly?

I did the class as @Push and the web.xml is configured correctly and inserted logs to track the execution and they are also running at the correct time.

Any suggestion?

I’m using Vaadin 8.5.1

Follow the code, thanks!

....

	final ProgressBar progressBar = new ProgressBar();

	@Override
	protected void criaPanel() {
...
		final ToolbarMigracao toolbar = new ToolbarMigracao();
		progressBar.setVisible(false);
....
		componentHorizontalLayout.addComponents(layoutDaToolbar, comboBoxVerticalLayout, datePickerVerticalLayout, progressBar);
...
		toolbar.getBtnMigrar().addClickListener(event -> {
			toolbar.getBtnMigrar().setEnabled(false);
			executar();
		});

...
}



	@Override
	protected void executar() {

...
		final AtomicInteger progressFinish = new AtomicInteger(0);
	    
		final ExecutorService threadPool =  new ThreadPoolExecutor(1, ConnectionConfig.getInstance().getMaximumThreadPoolSize(),
				1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(ConnectionConfig.getInstance().getMaximumThreadPoolSize()),
				Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
		
		final MigrationTransactionVO migrationVO = new MigrationTransactionVO();
		
		final UI ui = UI.getCurrent();
		final Set<Future<?>> listOfThreadsForExecution = new HashSet<Future<?>>();

		this.getCarteirasSelecionadas().forEach(s ->  {
			Future<?> taskStatus = threadPool.submit(()->{  
...
				try {
...
					ui.access(new Runnable() {
						@Override
						public void run() {
							updateProgressBar(ui, progressFinish.incrementAndGet(), listOfThreadsForExecution.size());
						}
					});
					
				} catch (Exception e) {
					log.error("Ocorreu um erro: " + e.getMessage(), e);
					Notification.show(MIGRATION, e.getMessage(), Notification.Type.ERROR_MESSAGE);
					
				}
				
			});
			listOfThreadsForExecution.add(taskStatus);
		});

		// executa as threads
		listOfThreadsForExecution.forEach(future -> {
			try {
				future.get();

			} catch (Exception e) {
				Notification.show(MIGRATION, e.getMessage(), Notification.Type.ERROR_MESSAGE);

			}
		});

		threadPool.shutdown();
		
	}

	private void updateProgressBar(final UI ui, final int progress, final int maxProgress) {
		ui.access(() -> {
			if(!progressBar.isVisible()) {
			    ui.setPollInterval(500);
				progressBar.setVisible(true);
				progressBar.setWidth("200px");
				progressBar.setCaption("Migrando ...");
				progressBar.setDescription("Descrição");
			}
			
			final float newValue;
			if (progress == maxProgress) {
				ui.setPollInterval(-1);
				progressBar.setVisible(false);
				newValue = 0f;
			} else {
				newValue = (float) progress / maxProgress;
			}
			progressBar.setValue(newValue);
			Notification.show("Executadas:", Integer.toString(progress) + " de " + maxProgress, Notification.Type.TRAY_NOTIFICATION).setDelayMsec(2000);
			ui.push();
		});
	}
	 


I see several issues in your code

Robson Ferreira:
Hi

I’m trying to implement a ProgressBar in a multithreaded routine, however it and Notification are only sensitized after running all threads. How do I get these components to run correctly?

I did the class as @Push and the web.xml is configured correctly and inserted logs to track the execution and they are also running at the correct time.

Any suggestion?

I’m using Vaadin 8.5.1

Follow the code, thanks!

....

	final ProgressBar progressBar = new ProgressBar();

	@Override
	protected void criaPanel() {
...
		final ToolbarMigracao toolbar = new ToolbarMigracao();
		progressBar.setVisible(false);
....
		componentHorizontalLayout.addComponents(layoutDaToolbar, comboBoxVerticalLayout, datePickerVerticalLayout, progressBar);
...
		toolbar.getBtnMigrar().addClickListener(event -> {
			toolbar.getBtnMigrar().setEnabled(false);
			executar();
		});

...
}



	@Override
	protected void executar() {

...
		final AtomicInteger progressFinish = new AtomicInteger(0);
	    
		final ExecutorService threadPool =  new ThreadPoolExecutor(1, ConnectionConfig.getInstance().getMaximumThreadPoolSize(),
				1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(ConnectionConfig.getInstance().getMaximumThreadPoolSize()),
				Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
		
		final MigrationTransactionVO migrationVO = new MigrationTransactionVO();
		
		final UI ui = UI.getCurrent();
		final Set<Future<?>> listOfThreadsForExecution = new HashSet<Future<?>>();

		this.getCarteirasSelecionadas().forEach(s ->  {
			Future<?> taskStatus = threadPool.submit(()->{  
...
				try {
...
					ui.access(new Runnable() {
						@Override
						public void run() {
							updateProgressBar(ui, progressFinish.incrementAndGet(), listOfThreadsForExecution.size())[mobile spy]

(https://www.webnovedad.com/mobile-spy-descarga-gratis-android-iphone/);

  				}
  			});
  			
  		} catch (Exception e) {
  			log.error("Ocorreu um erro: " + e.getMessage(), e);
  			Notification.show(MIGRATION, e.getMessage(), Notification.Type.ERROR_MESSAGE);
  			
  		}
  		
  	});
  	listOfThreadsForExecution.add(taskStatus);
  });

  // executa as threads
  listOfThreadsForExecution.forEach(future -> {
  	try {
  		future.get();

  	} catch (Exception e) {
  		Notification.show(MIGRATION, e.getMessage(), Notification.Type.ERROR_MESSAGE);

  	}
  });

  threadPool.shutdown();

}

private void updateProgressBar(final UI ui, final int progress, final int maxProgress) {
ui.access(() → {
if(!progressBar.isVisible()) {
ui.setPollInterval(500);
progressBar.setVisible(true);
progressBar.setWidth(“200px”);
progressBar.setCaption(“Migrando …”);
progressBar.setDescription(“Descrição”);
}

  	final float newValue;
  	if (progress == maxProgress) {
  		ui.setPollInterval(-1);
  		progressBar.setVisible(false);
  		newValue = 0f;
  	} else {
  		newValue = (float) progress / maxProgress;
  	}
  	progressBar.setValue(newValue);
  	Notification.show("Executadas:", Integer.toString(progress) + " de " + maxProgress, Notification.Type.TRAY_NOTIFICATION).setDelayMsec(2000);
  	ui.push();
  });

}

I have the same problem