Announcement

Collapse
No announcement yet.

Android - THREAD Sections.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Android - THREAD Sections.

    This example also applies to JAVA compilations. In fact i will show the results in a java console window.

    Tip: I have found that testing individual functions as Java console functions speeds up development, because
    they compile much faster.

    Code:
    [FONT=Courier New][B][COLOR=Blue]#COMPILE[/COLOR] [COLOR=Blue]JAR[/COLOR]
    [COLOR=Blue]#DIM[/COLOR] [COLOR=Blue]ALL[/COLOR]
    [COLOR=Blue]#REPORT[/COLOR] [COLOR=Blue]NONE[/COLOR]
    [COLOR=Blue]#ENCODE[/COLOR] [COLOR=Black]NO[/COLOR]
    [COLOR=Blue]#PROJECT[/COLOR] [COLOR=Blue]LOCAL[/COLOR]
    [COLOR=Blue]#VERSION[/COLOR] [COLOR=Blue]AUTO[/COLOR]
    [COLOR=Blue]#DATABASE[/COLOR] [COLOR=Blue]CLOSED[/COLOR]
    
    
    [COLOR=Blue]FUNCTION[/COLOR] [COLOR=Black]PBMAIN[/COLOR][COLOR=Black]([/COLOR][COLOR=Black])[/COLOR] [COLOR=Blue]AS[/COLOR] [COLOR=Blue]LONG[/COLOR]
    
    [COLOR=Blue]LOCAL[/COLOR] [COLOR=Black]Index[/COLOR] [COLOR=Blue]AS[/COLOR] [COLOR=Blue]LONG[/COLOR]
    [COLOR=Blue]DIM[/COLOR] [COLOR=Black]Product[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]5[/COLOR][COLOR=Black])[/COLOR] [COLOR=Blue]AS[/COLOR] [COLOR=Blue]LONG[/COLOR]
    [COLOR=Blue]DIM[/COLOR] [COLOR=Black]Threads[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]5[/COLOR][COLOR=Black])[/COLOR] [COLOR=Blue]AS[/COLOR] [COLOR=Blue]LONG[/COLOR]
    
    [COLOR=Blue]FOR[/COLOR] [COLOR=Black]Index[/COLOR] [COLOR=Black]=[/COLOR] [COLOR=Black]1[/COLOR] [COLOR=Blue]TO[/COLOR] [COLOR=Black]5[/COLOR]
        [COLOR=Silver]' Index becomes a THREADED variable.[/COLOR]
        [COLOR=Silver]'Its value will remain the same in the[/COLOR]
        [COLOR=Silver]'main thread after the thread section ends.[/COLOR]
        [COLOR=Blue]THREAD[/COLOR] [COLOR=Blue]SECTION[/COLOR] [COLOR=Blue]START[/COLOR] [COLOR=Black]Index[/COLOR] [COLOR=Blue]TO[/COLOR] [COLOR=Black]Threads[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Index[/COLOR][COLOR=Black])[/COLOR]
        
            [COLOR=Blue]THREADED[/COLOR] [COLOR=Black]Counter[/COLOR] [COLOR=Blue]AS[/COLOR] [COLOR=Blue]LONG[/COLOR]
            
            [COLOR=Blue]SLEEP[/COLOR] [COLOR=Blue]RND[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]1[/COLOR][COLOR=Black],[/COLOR] [COLOR=Black]200[/COLOR][COLOR=Black])[/COLOR] [COLOR=Silver]' Make them start at diferent times.[/COLOR]
            
            [COLOR=Blue]PRINTBR[/COLOR] [COLOR=DarkGreen]"Starting thread with ID "[/COLOR] [COLOR=Red]&[/COLOR] [COLOR=Blue]FORMAT$[/COLOR][COLOR=Black]([/COLOR][COLOR=Blue]THREADID[/COLOR][COLOR=Black])[/COLOR]
            
            [COLOR=Blue]FOR[/COLOR] [COLOR=Black]Counter[/COLOR] [COLOR=Black]=[/COLOR] [COLOR=Black]1[/COLOR] [COLOR=Blue]TO[/COLOR] [COLOR=Black]5000[/COLOR]            
                [COLOR=Blue]INCR[/COLOR] [COLOR=Black]Product[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Index[/COLOR][COLOR=Black])[/COLOR] [COLOR=Blue]BY[/COLOR] [COLOR=Black]1[/COLOR] [COLOR=Silver]' We still have access to main thread's arrays![/COLOR]
            [COLOR=Blue]NEXT[/COLOR] [COLOR=Black]Counter[/COLOR]
        
        [COLOR=Blue]END[/COLOR] [COLOR=Blue]THREAD[/COLOR]
        [COLOR=Blue]PRINTBR[/COLOR] [COLOR=DarkGreen]"Created thread with ID "[/COLOR] [COLOR=Red]&[/COLOR] [COLOR=Blue]FORMAT$[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Threads[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Index[/COLOR][COLOR=Black])[/COLOR][COLOR=Black])[/COLOR]
    [COLOR=Blue]NEXT[/COLOR] [COLOR=Black]Index[/COLOR]
    
    [COLOR=Blue]PRINTBR[/COLOR] [COLOR=DarkGreen]"Values before wait:"[/COLOR]
    [COLOR=Blue]FOR[/COLOR] [COLOR=Black]Index[/COLOR] [COLOR=Black]=[/COLOR] [COLOR=Black]1[/COLOR] [COLOR=Blue]TO[/COLOR] [COLOR=Black]5[/COLOR]
        [COLOR=Blue]PRINTBR[/COLOR] [COLOR=Blue]STR$[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Product[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Index[/COLOR][COLOR=Black])[/COLOR][COLOR=Black])[/COLOR]
    [COLOR=Blue]NEXT[/COLOR] [COLOR=Black]Index[/COLOR]
    
    [COLOR=Blue]THREAD[/COLOR] [COLOR=Blue]WAIT[/COLOR] [COLOR=Blue]ALL[/COLOR]
    
    [COLOR=Blue]PRINTBR[/COLOR] [COLOR=DarkGreen]"Values after wait:"[/COLOR]  
    [COLOR=Blue]FOR[/COLOR] [COLOR=Black]Index[/COLOR] [COLOR=Black]=[/COLOR] [COLOR=Black]1[/COLOR] [COLOR=Blue]TO[/COLOR] [COLOR=Black]5[/COLOR]
        [COLOR=Blue]PRINTBR[/COLOR] [COLOR=DarkGreen]"final value for thread "[/COLOR] [COLOR=Red]&[/COLOR] [COLOR=Blue]FORMAT$[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Threads[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Index[/COLOR][COLOR=Black])[/COLOR][COLOR=Black])[/COLOR] [COLOR=Red]&[/COLOR] [COLOR=DarkGreen]" is:"[/COLOR] [COLOR=Red]&[/COLOR] [COLOR=Blue]STR$[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Product[/COLOR][COLOR=Black]([/COLOR][COLOR=Black]Index[/COLOR][COLOR=Black])[/COLOR][COLOR=Black])[/COLOR]
    [COLOR=Blue]NEXT[/COLOR] [COLOR=Black]Index[/COLOR]        
    
    [COLOR=Blue]END[/COLOR] [COLOR=Blue]FUNCTION[/COLOR][/B][/FONT]
    Here is the Java console output:



    Note:
    Usually you can work with the same data in the threads without crashes, but the results are unreliable,
    it is recommended to work with a different data set (as in the example, where each thread works with a
    different array index) to get reliable results. This also applies to using Database commands with threads.
    It is not recommended to use GUI statements inside threads.

    Thread sections are available only for Java and Android compilations.
    www.patreon.com/pluribasic
Working...
X