CS_BOM_EXPL_MAT_V2 Problem
Hi All,
I am developing a report which displays items at bom level. But unable to do so using 'CS_BOM_EXP_MAT_V2' functional module.
Kindly tell me which are necessary parameters which are necessary for this functional module.
In display mode all parameters are optional but it is not working .
Regards,
EXP
Dear Vijay,
Sending u the sample program.The error is object not found.
Kindly help in this.
tables : afko,
makt,
afru,
stpo,
mast.
data : begin of it_result1 occurs 0,
werks like aufk- 1
matnr like mast-matnr,"material no 1
menge like stpo-menge,"total ordered qty 3
maktx like makt-maktx,"material description 2
vmvpr like mbew-vmvpr,"Price Control Indicator in Previous Period 4
verpr like mbew-verpr,"if the indicator is s then standard price else v then moving price 4
stprs like MBEW-STPRS, " 4
dmbtr like mseg-dmbtr, "actual concumption 5
aufnr like afko-aufnr,"pp order no
stlnr like afko-stlnr,"bom
idnrk like stpo-idnrk, " 3
end of it_result1 .
data : begin of it_stb occurs 0.
include structure STPOX.
data : end of it_stb.
selection-screen begin of block blk with frame title text-001.
select-options : s_werks for mast-werks obligatory. " plant
parameters : p_matnr like mast-matnr."material no
select-options : s_aufnr for afko-aufnr ."order no
selection-screen end of block blk.
select amatnr bmaktx cmenge dvmvpr dverpr dstprs estlnr estlan fwerks"edmbtr
into corresponding fields of table it_result1 from mast as a
inner join makt as b on amatnr = bmatnr
inner join stpo as c on astlnr = cstlnr
inner join mbew as d on amatnr = dmatnr
inner join afko as e on astlnr = estlnr
inner join aufk as f on eaufnr = faufnr
inner join jest as g on fobjnr eq gobjnr AND STAT EQ 'I0009'
where awerks in s_werks and amatnr = p_matnr
and f~aufnr in s_aufnr.
delete adjacent duplicates from it_result1 comparing matnr.
loop at it_result1.
write :/2 it_result1-matnr inverse color 7 on,it_result1-maktx inverse color 7 on , it_result1-aufnr.
endloop.
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
CAPID = 'X'
DATUV = sy-datum
MDMPS = 'X'
MEHRS = 'X'
MTNRV = it_result1-matnr
WERKS = it_result1-werks
MDNOT = 'X'
TABLES
stb = it_stb
Similar Messages
-
Help reqd in CS_BOM_EXPL_MAT_V2
Hi,
I'm using CS_BOM_EXPL_MAT_V2 to explode a BOM. Even though I have checked import parameter MEHRS = 'X', the BOM only explodes to the first level. Inshort the function module is not outputting the sub components under the components of the header material( i.e. level 2 and further)...
Does anyone know how to resolve this ?
Regards,
Aravindhi Aravind,
WELCOME TO SDN
Check these Links
'CS_BOM_EXPL_MAT_V2 ?
Re: CS_BOM_EXPL_MAT_V2 Problem
http://erp.ittoolbox.com/groups/technical-functional/sap-r3-dev/573891#
http://www.sap-img.com/abap/abap-clipboard-utilities-for-beautiful-commented-code.htm
Regards,
Santosh
Note: Reward Points if helpful -
BOM: Problem using CS_BOM_EXPL_MAT_V2 - URGENT!!!
hai friends,
i am using CS_BOM_EXPL_MAT_V2 fn module in my code. when i pass mtnrv = 'GK02PB.05SBIS' , i did' get any output from the above fn module. but other than the above value like mtnrv = 'OSA875CCWOF' ,i am getting output.
thanks in advance,
With regards,
senthil kumar.rHi senthil,
1. I suppose u want to explode the BOM.
2. CS_BOM_EXPLOSION
use the above FM
in the below mentioned fashion.
3.
CALL FUNCTION 'CS_BOM_EXPLOSION'
EXPORTING
capid = 'PP01'
emeng = bmeng
datuv = sy-datum
mtnrv = matnr
stlan = '1'
werks = werks
mehrs = 'X'
IMPORTING
TOPEQUI =
TOPMAT =
TOPTPL =
DSTST =
TABLES
stbd = stbd
stbe = stbe
stbk = stbk
stbm = stbm
stbp = stbp
stbt = stbt
EXCEPTIONS
alt_not_found = 1
call_invalid = 2
missing_authorization = 3
no_bom_found = 4
no_plant_data = 5
no_suitable_bom_found = 6
object_not_found = 7
conversion_error = 8
OTHERS = 9
regards,
amit m. -
Problem while using Alv Block List
HI All,
PLZ help me .
By using this code I select data from TXT file and insert data in CS13 transaction then I want to show each material which present in TXT file as ALV BLOCK DISPLAY.
Suppose txt file contain 4 material so report will show 4 list block .means data of every material showing in different blocks in the report.
Code which I am using showing showing data only for last material which present in the table it_matmaster in every block.
Please guide me what is the problem.
I am pasting changed code for you.
i am using below codes..........
*& Report ZCS13 *
REPORT zcs13 .
TABLES:
mara,
stpo,
t001w,
stko.
TYPE-POOLS: slis.
DATA: BEGIN OF it_stb OCCURS 0.
INCLUDE STRUCTURE zstpox.
DATA:END OF it_stb.
DATA: BEGIN OF it_matmaster OCCURS 0,
matnr TYPE matnr,
werks TYPE werks,
stlal type stlal,
capid type capid,
emeng TYPE emeng,
END OF it_matmaster.
DATA : ievent TYPE slis_t_event.
DATA: it_stb2 LIKE stpox OCCURS 0 WITH HEADER LINE,
it_stb3 LIKE stpox OCCURS 0 WITH HEADER LINE,
temp_cat TYPE slis_fieldcat_alv,
sline TYPE slis_listheader,
gt_list_top_of_page TYPE slis_t_listheader,
gs_layout TYPE slis_layout_alv ,
gt_events TYPE slis_t_event,
gt_sort TYPE slis_t_sortinfo_alv,
w_msg(255) TYPE c,
int TYPE i VALUE 0.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-002.
PARAMETERS:p_file LIKE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1 .
CONSTANTS c_x VALUE 'X'.
DATA: w_topmat LIKE cstmat,
fieldcat TYPE slis_t_fieldcat_alv ,
wa_fieldcat TYPE slis_fieldcat_alv.
DATA:v_file TYPE string.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
START-OF-SELECTION.
v_file = p_file.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = v_file
filetype = 'ASC'
has_field_separator = 'X'
HEADER_LENGTH = 0
READ_BY_LINE = 'X'
DAT_MODE = ' '
IMPORTING
FILELENGTH =
HEADER =
TABLES
data_tab = it_matmaster
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
EXPORTING
i_callback_program = 'ZCS13'
I_CALLBACK_PF_STATUS_SET = PFSTATUS
i_callback_user_command = ''
IT_EXCLUDING =
PERFORM fldcat.
LOOP AT it_matmaster.
PERFORM explode_assembly.
PERFORM fldcat.
PERFORM append_blocklist.
PERFORM alv_display_grid.
ENDLOOP.
PERFORM alv_display_grid.
*& Form EXPLODE_ASSEMBLY
text
FORM explode_assembly.
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
capid = 'BEST'
cuols = c_x
datuv = sy-datum
emeng = it_matmaster-emeng
knfba = c_x
ksbvo = c_x
mehrs = c_x
mbwls = c_x
mdmps = c_x
mktls = c_x
stlal = it_matmaster-stlal
stlan = p_bomap
mtnrv = it_matmaster-matnr
werks = it_matmaster-werks
svwvo = 'X'
vrsvo = 'X'
IMPORTING
topmat = w_topmat
TABLES
stb = it_stb
EXCEPTIONS
alt_not_found = 1
call_invalid = 2
material_not_found = 3
missing_authorization = 4
no_bom_found = 5
no_plant_data = 6
no_suitable_bom_found = 7
conversion_error = 8
OTHERS = 9.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO w_msg.
WRITE: / w_msg.
EXIT.
ENDIF.
ENDFORM. "explode_assembly
*& Form fldcat
text
--> p1 text
<-- p2 text
FORM fldcat .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = 'ZCS13'
i_internal_tabname = 'IT_STB'
i_structure_name = 'ZSTPOX'
I_CLIENT_NEVER_DISPLAY = 'X'
i_inclname = 'ZCS13'
I_BYPASSING_BUFFER =
I_BUFFER_ACTIVE =
CHANGING
ct_fieldcat = fieldcat[]
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " fldcat
*& Form alv_display_grid
text
--> p1 text
<-- p2 text
FORM alv_display_grid .
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
EXPORTING
i_interface_check = 'x_print_layout'
IS_PRINT =
I_SCREEN_START_COLUMN = 0
I_SCREEN_START_LINE = 0
I_SCREEN_END_COLUMN = 0
I_SCREEN_END_LINE = 0
IMPORTING
E_EXIT_CAUSED_BY_CALLER =
ES_EXIT_CAUSED_BY_USER =
EXCEPTIONS
program_error = 1
OTHERS = 2
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " alv_display_grid
*& Form append_blocklist
text
--> p1 text
<-- p2 text
FORM append_blocklist .
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
is_layout = gs_layout
it_fieldcat = fieldcat
i_tabname = 'IT_STB[]'
it_events = gt_events
it_sort = gt_sort
I_TEXT = ' '
TABLES
t_outtab = it_stb[]
EXCEPTIONS
program_error = 1
maximum_of_appends_reached = 2
OTHERS = 3
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " append_blocklistHi ,
Use the upload function 'WS_UPLOAD' . Declare ITAB of type table of your choice.
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
FILENAME = FILENAME( Has to specified by you )
FILETYPE = 'ASC'
TABLES
DATA_TAB = ITAB
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10.
LOOP AT ITAB.
Use Split function to seperate the fields of the Internal table and append to a new Internal table of your choice.
ENDLOOP. -
Refresh using CS_BOM_EXPL_MAT_V2 2 times
Hi,
my problem is:
1. use fm CS_BOM_EXPL_MAT_V2 to get BOM data.
2. I modify the BOM using a Batch Input
3. use fm CS_BOM_EXPL_MAT_V2 to get the BOM updated.
the FM in the 3th step load the old data and not the updated BOM.
Any suggest?
Can i clear the program cache?Hello,
I'm answering to this threat even though it's 5 years old just in case somebody is looking out for the solution like I was a couple of weeks ago.
The FM CS_BOM_EXPL_MAT_V2 calls internally another FM called GET_STPO. Inside GET_STPO you will find structure STPOCTL which will contain material's information for the previous run of CS_BOM_EXPL_MAT_V2. Also, FM GET_STPO calls FM GET_STATS which will contain the structure STASCTL; both these structures need to be cleared in order for the Function Module to retrieve fresh data from the database, instead of just returning the same data that was returned in the first run.
In the Function Group CSIO, which contains all these Function Modules, there's a Function Module in charge of clearing these structures plus another internal tables which will contain the material's information, you need to call this FM right after your 1st iteration in order to clear the buffer. The Function Module is CSIO_REFRESH_BUFFER or CS_IO_BOM_BUFFER_SMASH, depending on your SAP implementation.
Hope this helps anyone
Message was edited by: Matthew Billingham - changed formatting to make more readable -
CS13 & CS_BOM_EXPL_MAT_V2
Hello Everyone,
I am using CS13 and Functional Module CS_BOM_EXPL_MAT_V2 for multi level BOM explosion for final assembly.
But the number of components vary from both the results.
I mean if CS13 is giving me 100 entires , then Functional Module CS_BOM_EXPL_MAT_V2 is giving me 30 entries.
Why is this so if , both are performing the same function.
Regards,
VishalVishal,
Please go through this Post, i think this should solve your problem.
[BOM explosion parameters Posted by Mr. Prabhu|http://www.erpgenie.com/forum/viewtopic.php?f=1&t=11280]
Regards,
Prasobh -
Regarding CS_BOM_EXPL_MAT_V2 and table returned STB
Hi Gurus ,
I am facing one diffulty.
I am calling following FM
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
capid = ' '
datuv = pa_datga
mdmps = lv_dummy
mehrs = lv_mehr
mtnrv = ls_mast-matnr
stlal = ls_mast-stlal
stlan = ls_mast-stlan
svwvo = gc_x
werks = pa_werks
vrsvo = gc_x
IMPORTING
topmat = ls_bom_header
TABLES
stb = lt_pos
EXCEPTIONS
alt_not_found = 1
call_invalid = 2
material_not_found = 3
missing_authorization = 4
no_bom_found = 5
no_plant_data = 6
no_suitable_bom_found = 7
conversion_error = 8
OTHERS = 9.
I am doing multilevel & dummy Bom explosion .
One internal table is returned " stb = lt_pos"
But i am not able to relate which is assmbly & its sub components .
Also Dummy Bom assembly & components from the table returned .
May be stufe field is making some sence as level , But still couldnt find the realtion between all IDNRK .
Please help me to solve this puzzle .hi flavio,
this report is used for total raw material estimation for the fiascal year for particular material.
my exact requirement is on the selection screen i have material,year,target qty.
when user pressess execute i need to display all the raw materials (to lower bom level) required for the material
proportional to the target qty.
for this i have used functiuon module CS_BOM_EXPL_MAT_V2 and able to retrieve the lower bom
level raw materials and their quantities into ltab and root material and its subcomponents into mtab
in this raw materials used for sub comp[onents and for the root material may overlap but the recipe for the raw materials
depends on the bom of base quantity of root material,
here my problem is how to differentiate the raw materials belongs to root material and its subcomponents ,( to perform
calculations against the target quantity) which we r getting in ltab of fm after bom explosion.
anybody pls suggest me . -
Hi Experts,
I called the fun. module CS_BOM_EXPL_MAT_V2 in SE38.
While executing the function module, it is giving output for some types of material like (TP0331-).
But it is raising the exception ALT NOT FOUND for some types of material like 'TB1000-'
But for all types of material, it is giving correct output in SE37.
I gave the input parameter's,
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
CAPID = 'PP01'
DATUV = sy-datum
EMENG = '1'
MKTLS = 'X'
MEHRS = 'X'
MTNRV = 'TB1000-'
STLAL = '1'
STLAN = '1'
SVWVO = 'X'
WERKS = '0010'
VRSVO = 'X'
TABLES
stb = it_stpox
EXCEPTIONS
ALT_NOT_FOUND = 1
CALL_INVALID = 2
MATERIAL_NOT_FOUND = 3
MISSING_AUTHORIZATION = 4
NO_BOM_FOUND = 5
NO_PLANT_DATA = 6
NO_SUITABLE_BOM_FOUND = 7
CONVERSION_ERROR = 8
OTHERS = 9
Can you please suggest me, why for the particular types of material, these function module is executing correctly in SE37 not in SE38.
Whether i need to give some more inputs in SE38.
Please guide me,
Thanks in advance,
Vijay.Rhi,
i have done the code like this,
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
CAPID = 'PP01'
DATUV = sy-datum
EMENG = '1'
MKTLS = 'X'
MEHRS = 'X'
MTNRV = 'TB1000-'
STLAL = '01' """" i have changed the value as 01 instead of 1.
STLAN = '1'
SVWVO = 'X'
WERKS = '0010'
VRSVO = 'X'
TABLES
stb = it_stpox
EXCEPTIONS
ALT_NOT_FOUND = 1
CALL_INVALID = 2
MATERIAL_NOT_FOUND = 3
MISSING_AUTHORIZATION = 4
NO_BOM_FOUND = 5
NO_PLANT_DATA = 6
NO_SUITABLE_BOM_FOUND = 7
CONVERSION_ERROR = 8
OTHERS = 9
it is a small problem..
whether u need any more information..
please let me know.. still it is not working means,
paste your code.. i will check
regards
vijay -
Quantity not fetched properly from FM CS_BOM_EXPL_MAT_V2
HI,
I am using the FM CS_BOM_EXPL_MAT_V2 to get the BOM components at multiple levels. The problem that i am facing is that for some components if the quantity is 0.100 or 0.10 the function module is returning 10.000 and 1.000 respectively. But in CS12 we are getting the correct values for quantity 0.100 and 0.10 for the same components. I am using the function module like this.
CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'
EXPORTING
capid = c_zeng
datuv = l_datum
emeng = '1'
mehrs = c_x
mtnrv = wa_outline-matnr
werks = wa_outline-werks
TABLES
stb = t_stpo
EXCEPTIONS
alt_not_found = 1
call_invalid = 2
material_not_found = 3
missing_authorization = 4
no_bom_found = 5
no_plant_data = 6
no_suitable_bom_found = 7
conversion_error = 8
OTHERS = 9.
So let me know how to get the correct values for quantities from this FM. Is there any other Fm which gives the correct values for quantity without any conversion.
Regards,
Sowmya.I tried that also but still it shows the same value. Another thing that i found was in CS11 or CS12 that component is displayed as 0.100 or 0.010 but in CS03 that component is having a value of 1 and 1 only. I am not able to understand the reason.
Regards,
Sowmya. -
BOM Explosion using Function 'CS_BOM_EXPL_MAT_V2 '
Hi Gurus,
I am working on BOM and exploding the multilevel BOM using function CS_BOM_EXPL_MAT_V2.
I want to know what does 'field-RNDKZ' do in that.
Thanks!Using Function module CS_BOM_EXPL_MAT_V2
As already has been said, the RNDKZ field is used for rounding. If you fill with '1', it will never round.
Example (the problem I had):
Material A consist of et al. 0.008 piece Material AA.
Material AA consist of 1.0 pice Material AAA,
of 7.0 piece Material AAB,
of 6.0 piece Material AAC,
of 12.0 piece Material AAD.
CASE 1: You fill RNDKZ field with '1':
You get the correct result which is:
You need for producing 1 piece of Material A
0.008 piece Material AA,
0.008 * 1 = 0.008 piece of Material AAA,
0.008 * 7.0 = 0,056 piece of Material AAB,
0.008 * 6.0 = 0,048 piece of Material AAC.
0.008 * 12.0 = 0,096 piece of Material AAD.
CASE 2: You let RNDKZ field blank.
In your user profile for BOM field "Round of" is set to blank (table field TCSPR-AMEKZ, TA S_ALR_87004496).
You get following result (which is wrong)
You need for producing 1 piece of Material A
1 piece Material AA,
1.0 *1.0 = 1.0 piece of Material AAA,
1.0 * 7.0 = 7.0 piece of Material AAB,
1.0 * 6.0 = 6.0 piece of Material AAC.
1.0 * 12.0 = 12.0 piece of Material AAD.
The number of decimals is seen depends as well from how many decimals is set for unit of measurement. In this case the number of decimals for "piece" was set to zero. (TA CUNI).
If you let the field RNDKZ field blank, the user profile field "Round of" (table field TCSPR-AMEKZ) is used. -
BOM Explosion (CS_BOM_EXPL_MAT_V2) - inactive alternative BOM
Hi,
I need to display the alternative BOM entered in the selection screen even if the entered alternative is inactive. i.e. status is inactive. i have used FM - CS_BOM_EXPL_MAT_V2.
Kindly advise.
Regards.Using Function module CS_BOM_EXPL_MAT_V2
As already has been said, the RNDKZ field is used for rounding. If you fill with '1', it will never round.
Example (the problem I had):
Material A consist of et al. 0.008 piece Material AA.
Material AA consist of 1.0 pice Material AAA,
of 7.0 piece Material AAB,
of 6.0 piece Material AAC,
of 12.0 piece Material AAD.
CASE 1: You fill RNDKZ field with '1':
You get the correct result which is:
You need for producing 1 piece of Material A
0.008 piece Material AA,
0.008 * 1 = 0.008 piece of Material AAA,
0.008 * 7.0 = 0,056 piece of Material AAB,
0.008 * 6.0 = 0,048 piece of Material AAC.
0.008 * 12.0 = 0,096 piece of Material AAD.
CASE 2: You let RNDKZ field blank.
In your user profile for BOM field "Round of" is set to blank (table field TCSPR-AMEKZ, TA S_ALR_87004496).
You get following result (which is wrong)
You need for producing 1 piece of Material A
1 piece Material AA,
1.0 *1.0 = 1.0 piece of Material AAA,
1.0 * 7.0 = 7.0 piece of Material AAB,
1.0 * 6.0 = 6.0 piece of Material AAC.
1.0 * 12.0 = 12.0 piece of Material AAD.
The number of decimals is seen depends as well from how many decimals is set for unit of measurement. In this case the number of decimals for "piece" was set to zero. (TA CUNI).
If you let the field RNDKZ field blank, the user profile field "Round of" (table field TCSPR-AMEKZ) is used. -
I am trying to implement some kind of a server listening for requests. The listener part of the app, is a daemon thread that listens for connections and instantiates a handling daemon thread once it gets some. However, my problem is that i must be able to kill the listening thread at the user's will (say via a sto button). I have done this via the Sun's proposed way, by testing a boolean flag in the loop, which is set to false when i wish to kill the thread. The problem with this thing is the following...
Once the thread starts excecuting, it will test the flag, find it true and enter the loop. At some point it will LOCK on the server socket waiting for connection. Unless some client actually connects, it will keep on listening indefinatelly whithought ever bothering to check for the flag again (no matter how many times you set the damn thing to false).
My question is this: Is there any real, non-theoretical, applied way to stop thread in java safely?
Thank you in advance,
LeftyThis was one solution from the socket programming forum, have you tried this??
public Thread MyThread extends Thread{
boolean active = true;
public void run(){
ss.setSoTimeout(90);
while (active){
try{
serverSocket = ss.accept();
catch (SocketTimeoutException ste){
// do nothing
// interrupt thread
public void deactivate(){
active = false;
// you gotta sleep for a time longer than the
// accept() timeout to make sure that timeout is finished.
try{
sleep(91);
}catch (InterruptedException ie){
interrupt();
} -
A problem with Threads and MMapi
I am tring to execute a class based on Game canvas.
The problem begin when I try to Play both a MIDI tone and to run an infinit Thread loop.
The MIDI tone "Stammers".
How to over come the problem?
Thanks in advance
Kobi
See Code example below:
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
public class MainScreenCanvas extends GameCanvas implements Runnable {
private MainMIDlet parent;
private boolean mTrucking = false;
Image imgBackgound = null;
int imgBackgoundX = 0, imgBackgoundY = 0;
Player player;
public MainScreenCanvas(MainMIDlet parent)
super(true);
this.parent = parent;
try
imgBackgound = Image.createImage("/images/area03_bkg0.png");
imgBackgoundX = this.getWidth() - imgBackgound.getWidth();
imgBackgoundY = this.getHeight() - imgBackgound.getHeight();
catch(Exception e)
System.out.println(e.getMessage());
* starts thread
public void start()
mTrucking = true;
Thread t = new Thread(this);
t.start();
* stops thread
public void stop()
mTrucking = false;
public void play()
try
InputStream is = getClass().getResourceAsStream("/sounds/scale.mid");
player = Manager.createPlayer(is, "audio/midi");
player.setLoopCount(-1);
player.prefetch();
player.start();
catch(Exception e)
System.out.println(e.getMessage());
public void run()
Graphics g = getGraphics();
play();
while (true)
tick();
input();
render(g);
* responsible for object movements
private void tick()
* response to key input
private void input()
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
imgBackgoundX++;
if (imgBackgoundX > 0)
imgBackgoundX = 0;
if ((keyStates & RIGHT_PRESSED) != 0)
imgBackgoundX--;
if (imgBackgoundX < this.getWidth() - imgBackgound.getWidth())
imgBackgoundX = this.getWidth() - imgBackgound.getWidth();
* Responsible for the drawing
* @param g
private void render(Graphics g)
g.drawImage(imgBackgound, imgBackgoundX, imgBackgoundY, Graphics.TOP | Graphics.LEFT);
this.flushGraphics();
}You can also try to provide a greater Priority to your player thread so that it gains the CPU time when ever it needs it and don't harm the playback.
However a loop in a Thread and that to an infinite loop is one kind of very bad programming, 'cuz the loop eats up most of your CPU time which in turn adds up more delays of the execution of other tasks (just as in your case it is the playback). By witting codes bit efficiently and planning out the architectural execution flow of the app before start writing the code helps solve these kind of issues.
You can go through [this simple tutorial|http://oreilly.com/catalog/expjava/excerpt/index.html] about Basics of Java and Threads to know more about threads.
Regds,
SD
N.B. And yes there are more articles and tutorials available but much of them targets the Java SE / EE, but if you want to read them here is [another great one straight from SUN|http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html] .
Edited by: find_suvro@SDN on 7 Nov, 2008 12:00 PM -
Hi all,
I would like to ask you for a help. I need to write a small program at my university. I started to write a midlet which function would be to countdown time for sports activities. I woul like to start a new thread - the one that counts down - and at the same time make the main thread sleep. After the "countdown" thread finishes, the main thread wakes up and waits for user input. The problem is that when the "countdown" thread finishes his work, I've got Uncaught exception java/lang/NullPointerException. error and the midlet halts.
Below you can find the code
import java.lang.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class intervals extends MIDlet implements CommandListener
public Display ekran;
private SweepCanvas sweeper;
private Form rundy;
private TextField round0, round1, round2, round3, round4, round5, round6, round7, round8;
private long czas,x;
private Command exitCommand;
private Command addRound;
private Command delRound;
private Command start;
private TextField repeat;
private Form odliczanie;
private Alert ostrz;
Licznik thread;
String test;
StringItem test1;
int parz,i,j,k;
static int l;
int ilrund;
int ilpowt;
Item sec;
long sec1;
public intervals()
rundy = new Form("Interwały sportowe");
exitCommand = new Command("Wyjście", Command.EXIT, 2);
addRound = new Command("Dodaj","Dodaj rundę", Command.ITEM,1);
delRound = new Command("Usuń","Usuń ostatnią rundę", Command.ITEM,1);
start = new Command("Start", Command.ITEM,1);
odliczanie = new Form("Odliczanie");
TextField dodaj(TextField kolej)
kolej=new TextField("Podaj czas (s) rundy "+parz,null, 4, TextField.NUMERIC);//stworzenie nowej instancji do wybierania czasu trwania rundy
if(rundy.size()==0)
rundy.insert(rundy.size(),kolej);
else
rundy.insert(rundy.size()-1, kolej);
return kolej;
void odliczanie(TextField round)
monitor m=new monitor();
k=Integer.parseInt(round.getString());
ekran.setCurrent(odliczanie);
thread=new Licznik(k,odliczanie);
thread.start();
ekran.setCurrent(rundy);
public void startApp()// throws MIDletStateChangeException
rundy.deleteAll();
repeat = new TextField("Podaj ilość powtórzeń",null,1,TextField.NUMERIC);
rundy.addCommand(addRound);
rundy.addCommand(exitCommand);
rundy.setCommandListener(this);
Canvas obrazek = new MyCanvas();
ekran = Display.getDisplay(this);
ekran.setCurrent(obrazek);
czas=System.currentTimeMillis();
while (System.currentTimeMillis()<czas+1000)
continue;
ekran.setCurrent(rundy);
public void pauseApp()
public void destroyApp(boolean unconditional)
notifyDestroyed();
public void commandAction(Command c, Displayable s)
if (c == exitCommand)
destroyApp(false);
notifyDestroyed();
else if(c==addRound)
if(rundy.size()==0)//Sprawdzenie ilości elementów w celu poprawnego wyświetlania liczby rund w formie
parz=1;
else
parz=rundy.size();
switch(parz)
case 1:
round0=dodaj(round0);break;
case 2:
round1=dodaj(round1);break;
case 3:
round2= dodaj(round2);break;
case 4:
round3=dodaj(round3);break;
case 5:
round4=dodaj(round4);break;
default:
ostrz=new Alert("Uwaga","Maksymalna liczba rund wynosi 9", null, AlertType.INFO);
ostrz.setTimeout(3000);
ekran.setCurrent(ostrz);
if(rundy.size()==1)
rundy.append(repeat);
rundy.addCommand(start);
rundy.addCommand(delRound);
else if(c==delRound)
if(rundy.size()!=0)
rundy.delete(rundy.size()-2);
if (rundy.size()==1)
rundy.deleteAll();
if(rundy.size()==0)
rundy.removeCommand(delRound);
rundy.removeCommand(start);
else if(c==start)
ilrund=rundy.size()-1;
if(this.repeat.size()>0)
ilpowt=Integer.parseInt(this.repeat.getString());
ekran = Display.getDisplay(this);
for (i=1; i<=ilpowt;i++)
odliczanie= new Form("Odliczanie");
for (j=0;j<ilrund;j++)
switch(j)
case 0:
odliczanie(round0);
break;
case 1:
odliczanie(round1);
break;
case 2:
odliczanie(round2);
break;
case 3:
odliczanie(round3);
break;
case 4:
odliczanie(round4);
break;
case 5:
odliczanie(round5);
break;
case 6:
odliczanie(round6);
break;
case 7:
odliczanie(round7);
break;
case 8:
odliczanie(round8);
break;
class Licznik extends Thread
int czas1,k;
Form forma;
monitor m;
public Licznik(int k,Form formap)
czas1=k;
forma=formap;
public synchronized void run()
while(czas1>0)
forma.deleteAll();
forma.append("Czas pozostały (s): "+czas1);
try{Thread.sleep(1000);} catch(InterruptedException e){e.printStackTrace();}
czas1--;
if(czas1<=0)
m.put();
}and monitor class
public class monitor
boolean busy=false;
synchronized void get()
if(!busy)
try
wait();
}catch(InterruptedException e){e.printStackTrace();}
notify();
synchronized void put()
if(busy)
try
wait();
}catch(InterruptedException e){e.printStackTrace();}
busy=true;
notify();
}Can anybody help me with this?Groovemaker,
Your Licznik class has a member m of type monitor, which has not been instantiated (in other words is null) hence, when calling m.put() you get NullPointerException. Please also mind, that using Thread.sleep(1000) is not an accurate way of measuring time.
If I may, please use recommended for Java class naming conventions - some of your names use lower case, while other don't which is confusing to the reader.
Daniel -
Problem with threads within applet
Hello,
I got an applet, inside this applet I have a singleton, inside this singleton I have a thread.
this thread is running in endless loop.
he is doing something and go to sleep on and on.
the problem is,
when I refresh my IE6 browser I see more than 1 thread.
for debug matter, I did the following things:
inside the thread, sysout every time he goes to sleep.
sysout in the singleton constructor.
sysout in the singleton destructor.
the output goes like this:
when refresh the page, the singleton constructor loading but not every refresh, sometimes I see the constructor output and sometimes I dont.
The thread inside the singleton is giving me the same output, sometime I see more than one thread at a time and sometimes I dont.
The destructor never works (no output there).
I don't understand what is going on.
someone can please shed some light?
thanks.
btw. I am working with JRE 1.1
this is very old and big applet and I can't convert it to something new.Ooops. sorry!
I did.
public void start() {
public void stop() {
public void destroy() {
try {
resetAll();
Configuration.closeConnection();
QuoteItem.closeConnection();
} finally {
try {
super.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
Maybe you are looking for
-
Link from delivery to subcontracting purchase order
Hi Guys, anyone know how to pick up the link from a delivery (LIKP / LIPS) and the subcontracting purchase order (EKKO / EKPO) which was used to create it? I'm not sure such a link exists - the PO does not appear in the document flow of the delivery,
-
Q10 Will Not Upgrade to 10.2
This is my first time ever in a "tech" forum so if the problem I am having is answered somewhere on this forum, please accept my appolgies in advance for not looking closer My Q10 OS is 10.1.0.4189. I have been trying to update to the 10.2 since it
-
Recommended servers that will run Solaris 8 - Where have they all gone?
I've been struggling to install Solaris 8 HW02/02 on some Dell Poweredge kit (2GHz CPUs) with no success and even supermicro boxes. Does any one have know of any rack mount 1U / 2U servers that will install and Run. Must be 1-2 CPUs, lots or RAM, hav
-
Transferring music video to another PC
I just read the post where you can't burn the music videos/tv shows to cd and was wondering if there is a way to transfer them to my laptop from the desktop to be "permanently" on the laptop, not just thru the shared folder on the network I have set
-
Alter css stylesheet to change rollover colors
I know how to change the rollover colors for links that I've created myself using the inspector, than links/format tabs. But I'm customizing a blog using the notebook template, mostly because I liked the font that it used more than the other blogs. I