java - Issue accessing class variable from thread -


The purpose of the code given below is to structure the product object as a product for each product, Check the availability of the product (product.imageURL) on the array list, product image (product.imageURL), remove the products without images (remove the product from the array list 'product'), and the products with the available image Return array list

  package com.catgen.thread; Import java.util.ArrayList; Import java.util.Iterator; Import java.util.list; Import com.catgen.Product; Import com.catgen.Utils; Public Square ProductFilterThread Thread Extended {Personal Product Products; Private listing & lt; Products & gt; Products = new arrelisted & lt; Products & gt; (); Public ProductsFilterthread () {} Public ProductsFilterthread (Product Products) {this.product = Products; } Public Synchronized Zero AddProduct (Product Product) {System.out.println ("Add Before:" + Product Received (). Size ()); . Adding GetProducts () (Product); System.out.println ("after adding:" + product received (). Size ()); } Public Synchronized Zero Removal Product (Product Product) {System.out.println ("First Rim:" + Products (). Size ()); . GetProducts () (product) removal; System.out.println ("After the Rim:" + Products (). Size ()); } Public synchronization list & lt; Products & gt; GetProducts () {This return.products; } Public Synchronized Zero Set Products (List & lt; Products & gt; Products) {this.products = products; } Running Public Zero () {Boolean ImageExists = Util. Phile Axisists (this product.imuse url); If (ImageExists) {System.out.println (this.product.ImageURL); RemoveProduct (this.product); }} Public listing & lt; Products & gt; GetProductsWithImageOnly (List & lt; Products & gt; Products) {ProductFilterThread pft = null; {List & lt; ProductFilterThread & gt; Threads = new Arsuachi & lt; Product filterTrade & gt; (); For (product product: product) {pft = new product filter (product); AddProduct (product); Pft.start (); Threads.add (PFT); } Iterator & lt; ProductFilterThread & gt; Thread = Thread Dictionary (); While (ThreadItter.HessNext ()) {ProductFilterThread Thread = ThreadEiter.net} (); Thread.join (); }} Hold (exception e) {e.printStackTrace (); } System.out.println ("Total returned product =" + product received (). Size ()); Return product (); }}  

Calling Statement:

  displayProducts = new ProductFilterThread (). GetProductsWithImageOnly (displayProducts);  

Here, when addProductsWithImageOnly () is added to the product (product), the list of products is returned, but it is not like that (no products are returned) When removing the method () is called from a thread, the result is that the products are never removed without images. As a result, all the products are returned by the module or not, in which the images contained in the images are or not.

What can be the problem here?

Thanks in advance James.

You are creating a new ProductFilterThread each walking in the loop:

New productFilterathite (product);

Each product filterTrade own

  personal list & gt; Products & gt; Products = new arrelisted & lt; Products & gt; ();  

So when you, in the run method,

  removeProduct (this.product);  

You are removing the product from your own products example.

I suggest that you can design it differently, perhaps by giving products - the list should work with thread, as ProductFilterThread :

< Pre> private listings & lt; Products & gt; Products; Public ProductsFilterthread (list & lt; product & gt; products) {this.products = products; }

But I should say that in this way you should think carefully while working with many threads. You need to carefully synchronize access to data structures.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -