Hello Friends,

I have 6 to 7 threads which are running parallely. Thing is I have to put Thread.Sleep() between each threads. How Can I optimize it?

else if (__selectedString.equals("Master DataInput")) {                                    
                try {

                    if (RecordStore.listRecordStores() != null) {
                        System.out.println("IT FOUND RECORDSTORE ......");

                        RecordStore.deleteRecordStore("Company");
                        RecordStore.deleteRecordStore("Product");
                        RecordStore.deleteRecordStore("Model");
                        RecordStore.deleteRecordStore("Customer");
                        RecordStore.deleteRecordStore("Sales");

                        System.out.println("Delete All RecordStore");



                    } else {
                        System.out.println("IT NOT FOUND RECORD STORE");



                    }

                } catch (Exception e) {
                    e.printStackTrace();


                }


                sync_company scompany = new sync_company(this);
                scompany.sync_start_compnay();





                try {
                    Thread.sleep(2000);


                } catch (Exception e) {
                    e.printStackTrace();


                }


                sync_Product sProduct = new sync_Product(this);
                sProduct.sync_Product();




                try {
                    Thread.sleep(2000);


                } catch (Exception e) {
                    e.printStackTrace();


                }
                sync_Model obj_sync_model = new sync_Model(this);
                obj_sync_model.sync_model_start();



                try {
                    Thread.sleep(2000);


                } catch (Exception e) {
                    e.printStackTrace();


                }

                sync_customer obj_sync_customer = new sync_customer(this);
                obj_sync_customer.sync_customer_start();



                try {
                    Thread.sleep(2000);


                } catch (Exception e) {
                    e.printStackTrace();


                }
                sync_Sales obj_sync_sales = new sync_Sales(this);
                obj_sync_sales.sync_sales_start();



                try {
                    Thread.sleep(2000);
                    alert_master = new Alert("Master DataInput", "DONE SUCESSFULLY", null, AlertType.INFO);
                    alert_master.setTimeout(1000);

                    switchDisplayable(
                            alert_master, Menu_List);


                } catch (Exception e) {
                    e.printStackTrace();


                }


                                     
                // write post-action user code here
            }

Here the thing is we are using Wait Screen of NetBeans. From this window is coming out even though data is importing. we tried Thread.Alive() function for each thread. That was worst case that not working at all. How can I optimize code??

This code is working but thing is its not optimized.

Thank You,
Hakoo Desai.

Please answer the question of this question. This is about Thread concept of Core Java. If anyone not able to understand my question that ask me for clarification.

Here, I am repeating my question in possibly simple manner.
I am using WaitScreen(Component of Netbeans visual Midlet) which returns Successful if no exception occurs.

My basic task is to take data from Network and saving them into Record Store.
I put 4 Multiple CheckBox to Import 4 different data table from database.

Now According status of Checkbox I am starting new Thread with passing Runnable Objects.
Now I have to put Thread.Sleep(for Some specific time) between two Start() function. If I don't do that than WaitScreen will come out on pressing the Next button.

Here, I don't want make Thread sleep for specific time. If I do that than below consequences would happen.

If I am making Thread to Sleep for 3 Secs and Data takes 5 Secs then Waitscreen will come for 2 Secs Early.


If I am making Thread to Sleep for 5 Secs and Data takes 3 Secs then Waitscreen will come for 2 Secs Lately.

So, Actually I want, How much time would be taken by Threads, That much time should remain on Screen. And then come back to Menu.

Hope this makes clear.

Thanking You,
Hakoo Desai.

I do not have much experiences with threads unfortunately for me, but what you describing doesn't sound right. The WaitScreen should take on responsibility of executing all tasks and till it is done it should show an animation. To help you with thread management you should check this sample of ExecutorService

I was experiencing these things from past 5 months. And then I wrote here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.